Integer Break
Input:
2
Output:
1
Explanation:
2 = 1 + 1, 1 × 1 = 1.Input:
10
Output:
36
Explanation:
10 = 3 + 3 + 4, 3 × 3 × 4 = 36.Last updated
Input:
2
Output:
1
Explanation:
2 = 1 + 1, 1 × 1 = 1.Input:
10
Output:
36
Explanation:
10 = 3 + 3 + 4, 3 × 3 × 4 = 36.Last updated
class Solution:
def integerBreak(self, n: int) -> int:
if not n:
return n
p = [float('-inf')] * (n+1)
p[1] =1
for i in range(2,n+1):
for j in range(1,i):
p[i] = max(p[i], max(j,p[j]) * max(p[i-j],(i - j))) #不拆可能比拆得到数更大
return p[n]