您的位置:首页 > 编程语言 > Python开发

343. Integer Break

2017-06-22 22:39 316 查看
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 and not larger than 58.

class Solution(object):
def integerBreak(self, n):
"""
:type n: int
:rtype: int
"""
dp = [0, 0, 1]
for i in xrange(3, n+1):
max_product = i - 1
for j in xrange(2, i // 2+1):
max_product = max(max_product, max(j, dp[j]) * max(i - j, dp[i - j]))
dp.append(max_product)
return dp
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  leetcode python