[LeetCode] 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.html

Example 1:git

Input: 2
Output: 1 Explanation: 2 = 1 + 1, 1 × 1 = 1.

Example 2:github

Input: 10
Output: 36 Explanation: 10 = 3 + 3 + 4, 3 × 3 × 4 = 36.

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

Credits:
Special thanks to @jianchao.li.fighter for adding this problem and creating all test cases.less

 

这道题给了咱们一个正整数n,让拆分红至少两个正整数之和,使其乘积最大。最简单粗暴的方法天然是检查全部状况了,可是拆分方法那么多,怎么才能保证能拆分出全部的状况呢?感受有点像以前那道 Coin Change,当前的拆分方法须要用到以前的拆分值,这种重现关系就很适合动态规划 Dynamic Programming 来作,咱们使用一个一维数组 dp,其中 dp[i] 表示数字i拆分为至少两个正整数之和的最大乘积,数组大小为 n+1,值均初始化为1,由于正整数的乘积不会小于1。能够从3开始遍历,由于n是从2开始的,而2只能拆分为两个1,乘积仍是1。i从3遍历到n,对于每一个i,须要遍历全部小于i的数字,由于这些都是潜在的拆分状况,对于任意小于i的数字j,首先计算拆分为两个数字的乘积,即j乘以 i-j,而后是拆分为多个数字的状况,这里就要用到 dp[i-j] 了,这个值表示数字 i-j 任意拆分可获得的最大乘积,再乘以j就是数字i可拆分获得的乘积,取两者的较大值来更新 dp[i],最后返回 dp[n] 便可,参见代码以下:post

 

解法一:this

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

 

题目提示中让用 O(n) 的时间复杂度来解题,并且告诉咱们找7到 10 之间的规律,那么咱们一点一点的来分析:url

正整数从1开始,可是1不能拆分红两个正整数之和,因此不能当输入。spa

那么2只能拆成 1+1,因此乘积也为1。code

数字3能够拆分红 2+1 或 1+1+1,显然第一种拆分方法乘积大为2。

数字4拆成 2+2,乘积最大,为4。

数字5拆成 3+2,乘积最大,为6。

数字6拆成 3+3,乘积最大,为9。

数字7拆为 3+4,乘积最大,为 12。

数字8拆为 3+3+2,乘积最大,为 18。

数字9拆为 3+3+3,乘积最大,为 27。

数字10拆为 3+3+4,乘积最大,为 36。

....

那么经过观察上面的规律,咱们能够看出从5开始,数字都须要先拆出全部的3,一直拆到剩下一个数为2或者4,由于剩4就不用再拆了,拆成两个2和不拆没有意义,并且4不能拆出一个3剩一个1,这样会比拆成 2+2 的乘积小。这样咱们就能够写代码了,先预处理n为2和3的状况,而后先将结果 res 初始化为1,而后当n大于4开始循环,结果 res 自乘3,n自减3,根据以前的分析,当跳出循环时,n只能是2或者4,再乘以 res 返回便可:

 

解法二:

class Solution {
public:
    int integerBreak(int n) {
        if (n == 2 || n == 3) return n - 1;
        int res = 1;
        while (n > 4) {
            res *= 3;
            n -= 3;
        }
        return res * n;
    }
};

 

咱们再来观察上面列出的 10 以前数字的规律,咱们还能够发现数字7拆分结果是数字4的三倍,而7比4正好大三,数字8拆分结果是数字5的三倍,而8比5大3,后面都是这样的规律,那么咱们能够把数字6以前的拆分结果都列举出来,而后以后的数经过查表都能计算出来,参见代码以下;

 

解法三:

class Solution {
public:
    int integerBreak(int n) {
        vector<int> dp{0, 0, 1, 2, 4, 6, 9};
        for (int i = 7; i <= n; ++i) {
            dp.push_back(3 * dp[i - 3]);
        }
        return dp[n];
    }
};

 

下面这种解法是热心网友留言告诉博主的,感受很叼,故而补充上来。是解法一的一种变形写法,再也不使用 while 循环了,而是直接分别算出能拆出3的个数和最后剩下的余数2或者4,而后直接相乘获得结果,参见代码以下:

 

解法四:

class Solution {
public:
    int integerBreak(int n) {
        if (n == 2 || n == 3) return n - 1;
        if (n == 4) return 4;
        n -= 5;
        return (int)pow(3, (n / 3 + 1)) * (n % 3 + 2);
    }
};

 

Github 同步地址:

https://github.com/grandyang/leetcode/issues/343

 

参考资料:

https://leetcode.com/problems/integer-break/

https://leetcode.com/problems/integer-break/discuss/80694/Java-DP-solution

https://leetcode.com/problems/integer-break/discuss/80785/O(log(n))-Time-solution-with-explanation

https://leetcode.com/problems/integer-break/discuss/80720/Easy-to-understand-C%2B%2B-with-explanation

https://leetcode.com/problems/integer-break/discuss/80689/A-simple-explanation-of-the-math-part-and-a-O(n)-solution

 

LeetCode All in One 题目讲解汇总(持续更新中...)

相关文章
相关标签/搜索