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.
给一个大于2的整数,将这个整数分成两个以上数字的和,使他们乘积最大
python answer 1:
class Solution(object):
def integerBreak(self, n):
"""
:type n: int
:rtype: int
"""
if n <= 3:
return n-1
if (n-2) % 3 == 0:
return 3**((n-2)/3)*2
if (n-3) % 3 == 0:
return 3**(n/3)
if (n-4) % 3 == 0:
return 3**((n-4)/3)*4
Python answer 2:
class Solution(object):
def integerBreak(self, n):
"""
:type n: int
:rtype: int
"""
dp = [0, 0, 1]
for i in range(3, n+1):
dp.append(0)
for j in range(i):
dp[i] = max(dp[i], max(dp[i-j], i-j)*max(dp[j], j))
return dp[n]