Integer Broker

作者: luomoxyz | 来源:发表于2016-04-26 15:03 被阅读9次

    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]
    

    相关文章

      网友评论

        本文标题:Integer Broker

        本文链接:https://www.haomeiwen.com/subject/owwqrttx.html