您的位置:首页 > 其它

Leetcode 343. Integer Break

2016-05-11 11:54 225 查看
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.

分析前10个数

最大值 分解

2 2=1+1

2 3=1+2

4 4=2+2

6 5=2+3

9 6=3+3

12 7=3+4

18 8=3+3+2

27 9=3+3+3

36 10=3+3+4

经过分析可得当3越多,值则越大

代码如下:

public class Solution {

public int integerBreak(int n) {

if(n ==2){

return 1;

}if(n == 3){

return 2;

}

int num = 1;

while(n > 4){

num *= 3;

n -= 3;

}

return num*n;

}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: