Welcome to Subscribe On Youtube

Question

Formatted question description: https://leetcode.ca/all/343.html

 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.

 @tag-dp
 @tag-math

Algorithm

Iteration

Positive integers start from 1, but 1 cannot be split into the sum of two positive integers, so it cannot be used as an input.

Then 2 can only be split into 1+1, so the product is also 1.

The number 3 can be split into 2+1 or 1+1+1. Obviously, the product of the first split method is two.

The number 4 is divided into 2+2, and the product is the largest, which is 4.

The number 5 is divided into 3+2, and the product is the largest, which is 6.

The number 6 is divided into 3+3, and the product is the largest, which is 9.

The number 7 is divided into 3+4, and the product is the largest, which is 12.

The number 8 is divided into 3+3+2, and the product is the largest, which is 18.

The number 9 is divided into 3+3+3, and the product is the largest, 27.

The number 10 is divided into 3+3+4, the product is the largest, 36.

Starting from 5, all numbers need to be divided into 3 first, until the remaining number is 2 or 4, because there is no need to divide the remaining 4, it does not make sense to divide into two 2 and not divide, and 4 cannot be divided into 3+1, which will be smaller than the product of 2+2.

So, first preprocess the case where n is 2 and 3, and then initialize the result res to 1, and then start the loop when n is greater than 4, multiply res by 3, and subtract 3 from n. According to the previous analysis, when out of the loop, n only Can be 2 or 4, then multiply by res to return.

DP

dp[i] represents the maximum product of the sum of at least two positive integers divided from i int[] dp = new int[n + 1];

And formula:

dp[i] = Math.max(
    dp[i],
    Math.max(
        j * (i - j),
        j * dp[i - j]
    )

Code

Java

  • 
    public class Integer_Break {
    
        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;
    
            }
        }
    
        class Solution_dp {
            public int integerBreak(int n) {
    
                // Among them, dp[i] represents the maximum product of the sum of at least two positive integers divided from i
                int[] dp = new int[n + 1];
                dp[0] = 1;
                dp[1] = 1;
                dp[2] = 1;
    
                for (int i = 3; i <= n; ++i) {
                    for (int j = 1; j < i; ++j) {
                        dp[i] = Math.max(
                            dp[i],
                            Math.max(
                                j * (i - j),
                                j * dp[i - j]
                            )
                        );
                    }
                }
                return dp[n];
            }
        }
    }
    
  • // OJ: https://leetcode.com/problems/integer-break/
    // Time: O(N * sqrt(N))
    // Space: O(N)
    class Solution {
    public:
        int integerBreak(int n) {
            vector<int> memo(59, 0);
            memo[1] = 1;
            for (int i = 2; i <= n; ++i) 
                for (int j = 1, b = ceil(sqrt(i)); j <= b; ++j)
                    memo[i] = max(memo[i], max(j, memo[j]) * max(i - j, memo[i - j]));
            return memo[n];
        }
    };
    
  • class Solution:
        def integerBreak(self, n: int) -> int:
            if n < 4:
                return n - 1
            if n % 3 == 0:
                return pow(3, n // 3)
            if n % 3 == 1:
                return pow(3, n // 3 - 1) * 4
            return pow(3, n // 3) * 2
    
    ############
    
    class Solution(object):
      def integerBreak(self, n):
        """
        :type n: int
        :rtype: int
        """
        if n <= 3:
          return n - 1
        if n % 3 == 0:
          return 3 ** (n / 3)
        if n % 3 == 1:
          return 3 ** ((n / 3) - 1) * 4
        if n % 3 == 2:
          return 3 ** (n / 3) * 2
    
    

All Problems

All Solutions