leetcode343. 整数拆分(dp)

给定一个正整数 n,将其拆分为至少两个正整数的和,并使这些整数的乘积最大化。 返回你可以获得的最大乘积。

示例 1:

输入: 2
输出: 1
解释: 2 = 1 + 1, 1 × 1 = 1。

class Solution {
    public int integerBreak(int n) {

        int[] dp=new int[n+1];
        dp[1]=1;
        for(int i=2;i<=n;i++)
        {
            for(int j=1;j<=(i-1)/2+1;j++)
            {
                     dp[i]=Math.max(dp[i], Math.max(dp[i-j],i-j)* Math.max(dp[j],j));
            }

        }
        return dp[n];
    }
}

猜你喜欢

转载自blog.csdn.net/weixin_44560620/article/details/107687861
今日推荐