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

相關文章
相關標籤/搜索