整数拆分,LeetCode 343题 ,Java实现

整数拆分问题,

求最有值,具有最优子结构,具有重叠自问题 --->符合动态规划模型

通过自底向下的方式,把计算过的自问题结果保存起来,减少运算次数。



/**
 * 整数划分问题
 * 动态规划,
 * 要分析清楚,
 * 几个特点:1,求最优值,2,最优子结构,3,重叠子问题
 * 子问题的答案先存起来
 */
public class Main343 {
    public int integerBreak(int n) {

        if (n == 2){
            return 1;
        }else if (n == 3){
            return 2;
        }
        int[] max = new int[n+1];
        max[1] = 1;
        max[2] = 2;
        max[3] = 3;
        int ans = 0;
        for (int i = 4; i <= n; i++){
            ans = 0;
            for (int j = 1; j <= i/2 + 1; j++){
                int cur = max[j] * max[i-j];
                if (ans < cur){
                    ans = cur;
                    max[i] = ans;
                }

            }
        }
        ans = max[n];
        return ans;

    }



    
}

猜你喜欢

转载自blog.csdn.net/aa792978017/article/details/89166350