343. Integer Break
- Total Accepted: 40852
- Total Submissions: 89682
- Difficulty: Medium
- Contributor: LeetCode
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 and not larger than 58.
题解:首先考虑当一个正整数分成两个正整数时如何做到最大,易得偶数时是两个n/2,奇数时是(n-1)/2 和 (n+1)/2,而当这两个分出来的数相乘大于原来的n时,那么n就应该分解,由此得出n>=4 和n>=5。就是说最终得到的分解的因子不能大于4,1是没有意义的,所以分解的因子只能为2和3,并且2 * 2 * 2 < 3 * 3,所以2的个数不能超过2个。根据这些限制,代码如下: