Skip to content

Latest commit

 

History

History
51 lines (34 loc) · 1.36 KB

File metadata and controls

51 lines (34 loc) · 1.36 KB

343. Integer Break - 整数拆分

给定一个正整数 n,将其拆分为至少两个正整数的和,并使这些整数的乘积最大化。 返回你可以获得的最大乘积。

示例 1:

输入: 2
输出: 1
解释: 2 = 1 + 1, 1 × 1 = 1。

示例 2:

输入: 10
输出: 36
解释: 10 = 3 + 3 + 4, 3 × 3 × 4 = 36。

说明: 你可以假设 不小于 2 且不大于 58。


题目标签:Math / Dynamic Programming

题目链接:LeetCode / LeetCode中国

题解

Language Runtime Memory
python3 36 ms N/A
class Solution:
    def integerBreak(self, n):
        """
        :type n: int
        :rtype: int
        """
        tmp = [0, 0, 1, 2, 4, 6, 9]
        if n < 7:
            return tmp[n]
        else:
            for i in range(7, n+1):
                tmp.append(3*tmp[i-3])
            return tmp[-1]