Leetcode - 343. Integer Break (分割整数)

Given a positive integer n, break it into the sum of at leasttwo positive integers and maximize the product of those integers. Return the maximum product you can get.

Example 1:

Input: 2
Output: 1
Explanation: 2 = 1 + 1, 1 × 1 = 1.

Example 2:

Input: 10
Output: 36
Explanation: 10 = 3 + 3 + 4, 3 × 3 × 4 = 36.

Note: You may assume that n is not less than 2 and not larger than 58.

def integerBreak(n):

    f = [0 for i in range(n+1)]
    
    f[2] = 1

    for i in range(3,n+1):
        for j in range(1,i//2+1):
            f[i] = max(f[i],max(f[j],j)*max(f[i-j],i-j))

    return f[n]

 

上一篇:混淆矩阵(Confusion Matrix)


下一篇:性能指标