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:
There is a simple O(n) solution to this problem.
You may check the breaking results of n ranging from 7 to 10 to discover the regularities.
思路参考自http://blog.csdn.net/liyuanbhu/article/details/51198124,最终完成的代码实际上是O(n)的(阶乘)。
证明思路也很简单,但是
e
<script type="math/tex" id="MathJax-Element-7">e</script>为最优解的证明仍然没有搞出来。
class Solution {
public:
int integerBreak(int n) {
int res = 0;
vector<int> data{1,1,1,2,4,6,9};
if (n <= 6){
return data[n];
}else {
int discuss = n/3, mod = n%3;
if (mod == 1){
discuss -= 1;
mod += 3;
}else {
}
return pow(3,discuss)*(mod ? mod : 1);
}
}
};