Leetcode: Integer Break

Posted neverlandly

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Leetcode: Integer Break相关的知识,希望对你有一定的参考价值。

1 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.
2 
3 For example, given n = 2, return 1 (2 = 1 + 1); given n = 10, return 36 (10 = 3 + 3 + 4).
4 
5 Note: You may assume that n is not less than 2 and not larger than 58.

O(N^2)解法: DP

dp[i] represent the maximum product of breaking up integer i

 1 public class Solution {
 2     public int integerBreak(int n) {
 3         int[] dp = new int[n+1];
 4         dp[1] = 1;
 5         for (int i=2; i<=n; i++) {
 6             for (int j=1; j<=i-1; j++) {
 7                 dp[i] = Math.max(dp[i], Math.max(j, dp[j]) * Math.max(i-j, dp[i-j]));
 8             }
 9         }
10         return dp[n];
11     }
12 }

O(N)解法:

the best factor is 3. we keep breaking n into 3‘s until n gets smaller than 10, then solve the problem by brute-force.

1 public class Solution {
2     public int integerBreak(int n) {
3         if (n == 2) return 1;
4         if (n == 3) return 2;
5         if (n == 4) return 4;
6         if (n == 5) return 6;
7         return 3*Math.max(n-3, integerBreak(n-3));
8     }
9 }

Why 3 is the best factor?

I saw many solutions were referring to factors of 2 and 3. But why these two magic numbers? Why other factors do not work?
Let‘s study the math behind it.

For convenience, say n is sufficiently large and can be broken into any smaller real positive numbers. We now try to calculate which real number generates the largest product.
Assume we break n into (n / x) x‘s, then the product will be xn/x, and we want to maximize it.

Taking its derivative gives us n * x^(n/x-2) * (1 - ln(x)).
The derivative is positive when 0 < x < e, and equal to 0 when x = e, then becomes negative when x > e,
which indicates that the product increases as x increases, then reaches its maximum when x = e, then starts dropping.

This reveals the fact that if n is sufficiently large and we are allowed to break n into real numbers,
the best idea is to break it into nearly all e‘s.
On the other hand, if n is sufficiently large and we can only break n into integers, we should choose integers that are closer to e.
The only potential candidates are 2 and 3 since 2 < e < 3, but we will generally prefer 3 to 2. Why?

Of course, one can prove it based on the formula above, but there is a more natural way shown as follows.

6 = 2 + 2 + 2 = 3 + 3. But 2 * 2 * 2 < 3 * 3.
Therefore, if there are three 2‘s in the decomposition, we can replace them by two 3‘s to gain a larger product.

All the analysis above assumes n is significantly large. When n is small (say n <= 10), it may contain flaws.
For instance, when n = 4, we have 2 * 2 > 3 * 1.










以上是关于Leetcode: Integer Break的主要内容,如果未能解决你的问题,请参考以下文章

Leetcode: Integer Break

Leetcode 343. Integer Break

leetcode [343]Integer Break

[leetcode] 343. Integer Break

leetcode@ [343] Integer Break (Math & Dynamic Programming)

343 Integer Break 整数拆分