LeetCodeLeetCode solutionsLeetcode

Integer Broker

2016-04-26  本文已影响9人  luomoxyz

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.
Show Hint **
Credits:Special thanks to @jianchao.li.fighter for adding this problem and creating all test cases.

Subscribe to see which companies asked this question

Show Tags

answer

class Solution(object):
    def integerBreak(self, n):
        """
        :type n: int
        :rtype: int
        """
        if n <= 3:  
           return n - 1
        dp = [1] * (n + 1)
        dp[2] = 2
        dp[3] = 3
        for each in xrange(4, n + 1):
              dp[i] = max(dp[i - 2] * 2, dp[i - 3] * 3)
        return dp[n]
上一篇 下一篇

猜你喜欢

热点阅读