343. Integer Break

Given a positive integern, break it into the sum ofat leasttwo positive integers and maximize the product of those integers. Return the maximum product you can get.
For example, givenn= 2, return 1 (2 = 1 + 1); givenn= 10, return 36 (10 = 3 + 3 + 4).
Note: You may assume thatnis not less than 2 and not larger than 58.
Hint: There is a simple O(n) solution to this problem.
You may check the breaking results ofnranging from 7 to 10 to discover the regularities.

代码简单, 分析过程很有趣! 
当一个数拆分成两个的时候乘积最大情况, 偶数时:  n/2 * n/2 >= n , n的最小值 4; 奇数时: (n+1)/2 * (n-1)/2  >= n; n==5 , 也就是说当一个数大于等于4的时候 应该有更好的拆分方法。 所以选择因子应该在1,2,3 中选择, 显然1 不需要选, 而同时 3*3 > 2*2*2.  所以尽量拆分成3 就会得到最大乘积。  

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容