343. Integer Break

来源:互联网 发布:淘宝哪家情侣装好看 编辑:程序博客网 时间:2024/06/03 17:02

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.

For example, given n = 2, return 1 (2 = 1 + 1); given n = 10, return 36 (10 = 3 + 3 + 4).

Note: you may assume that n is not less than 2.

Hint:

  1. There is a simple O(n) solution to this problem.
  2. You may check the breaking results of n ranging from 7 to 10 to discover the regularities.

1.我的解答:
任何一个数都可以表示成3和2的和。并且由于3*1 < 2*2,因此当遇到3 1时,就把它变成2 2.如:
4 -> 2*2
5 -> 2*3
6 -> 3*3
7 -> 3*2*2
8 -> 3*3*2
9 -> 3*3*3
由上得,对整数不断用3去减,若最后是1,则表示最后两个是3*1,则需把他变成2*2.以下是我的代码:
class Solution {public:    int integerBreak(int n) {        int chen = n;        int sum = 1;    if(n <= 2)    return 1;    if(n == 3)    return 2;     while(1){         if(chen < 3)         break;         chen = chen-3;         sum = sum*3;     }     if(chen == 1){         sum = sum / 3;         sum = sum * 2 * 2;     }     if(chen == 2){         sum = sum * 2;     }     return sum;    }};

2.别人的答案,对此题的解释。

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 * xn/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. To fix it, we keep breaking n into3's until n gets smaller than 4.

3.另一个人的解释

Given a number n lets say we have a possible product P = p1 * p2 * ... pk. Then we notice what would happen if we could break pi up into two more terms lets say one of the terms is 2 we would get the terms pi-2 and 2 so if 2(pi-2) > pi we would get a bigger product and this happens if pi > 4. since there is one other possible number less then 4 that is not 2 aka 3. Likewise for 3 if we instead breakup the one of the terms into pi-3 and 3 we would get a bigger product if 3*(pi-3) > pi which happens if pi > 4.5.

Hence we see that all of the terms in the product must be 2's and 3's. So we now just need to write n = a3 + b2 such that P = (3^a) * (2^b) is maximized. Hence we should favor more 3's then 2's in the product then 2's if possible.

So if n = a*3 then the answer will just be 3^a.

if n = a*3 + 2 then the answer will be 2(a^3).

and if n = a*3 + 2*2 then the answer will be 2 * 2 * (a^3)



0 0
原创粉丝点击