其他分享
首页 > 其他分享> > Leetcode - 343. Integer Break (分割整数)

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]

 

标签:Output,max,36,Break,range,343,Integer,Input,positive
来源: https://blog.csdn.net/weixin_41362649/article/details/100022882