[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.

Example 1:

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

Example 2:

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.

 

這道題給了我們一個正整數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] 即可,參見代碼如下:

 

解法一:

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 之間的規律,那么我們一點一點的來分析:

正整數從1開始,但是1不能拆分成兩個正整數之和,所以不能當輸入。

那么2只能拆成 1+1,所以乘積也為1。

數字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 題目講解匯總(持續更新中...)


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM