343. Integer Break

Given a positive integer n, break it into the sum of at least two positive integers and maximize the product of those integers. Return the maximum product you can get.

For example, given n = 2, return 1 (2 = 1 + 1); given n = 10, return 36 (10 = 3 + 3 + 4).

Note: You may assume that n is not less than 2 and not larger than 58.

class Solution {
public:
    int integerBreak(int n) {
        vector<int> maxPro(n+1, 0);
        if(n == 1) return 0;
        maxPro[1] = 0;
        maxPro[2] = 1;
        for(int i = 3; i <= n; i++){
            for(int j = 1; j < i; j++){
                maxPro[i] = max(maxPro[i], max(j*(i-j), j*maxPro[i-j]));
            }
        }
        return maxPro[n];
    }
};

阅读更多
版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/u013250416/article/details/80330217
个人分类: leetcode
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

不良信息举报

343. Integer Break

最多只允许输入30个字

加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!
关闭
关闭