您的位置:首页 > 其它

leetcode 343. Integer Break

2016-05-26 13:39 225 查看
题目链接:https://leetcode.com/problems/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.

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.

题目大意:把一个数拆分成多个整数,求拆分后最大乘积。

我们要找拆分后的最大乘积必须要找到更多的3的组合,证明就算了。然后后面的事就很好办了。代码:

public class Solution {
public int integerBreak(int n) {
if(n < 4) return n-1;
int res = 1;
while(n > 2){
res *= 3;
n -= 3;
}
if(n == 0) return res;
else if(n == 1) return (res / 3 ) * 4;
else return res * 2;
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  leetcode