考点 | 难度 |
---|---|
DP | Medium |
Given an integer n
, break it into the sum of k
positive integers, where k >= 2
, and maximize the product of those integers.
Return the maximum product you can get.
例:n = 8
, result = 2 * (3 * 3)
, (3 * 3)
是n = 6
的result,2
是n = 2
的result
所以把n
拆成两个值i
和j
,选i/j
(i/j
不拆) 和dp[i/j]
(拆i/j
) 的更大值相乘
class Solution(object):
def integerBreak(self, n):
dp = [None, 1]
for m in range (2, n + 1):
j = m - 1
i = 1
max_product = 0
while i <= j:
max_product = max(max_product, max(i, dp[i]) * max(j, dp[j]))
j -= 1
i += 1
dp.append(max_product)
return dp[n]