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.git
Example 1:github
Input: 2
Output: 1
Explanation: 2 = 1 + 1, 1 × 1 = 1.
Example 2:app
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.less
給定一個正整數 n,將其拆分爲至少兩個正整數的和,並使這些整數的乘積最大化。 返回你能夠得到的最大乘積。優化
示例 1:ui
輸入: 2
輸出: 1
解釋: 2 = 1 + 1, 1 × 1 = 1。
示例 2:code
輸入: 10
輸出: 36
解釋: 10 = 3 + 3 + 4, 3 × 3 × 4 = 36。
說明: 你能夠假設 n 不小於 2 且不大於 58。ip
# -*- coding: utf-8 -*- # @Author: 何睿 # @Create Date: 2019-04-08 17:35:39 # @Last Modified by: 何睿 # @Last Modified time: 2019-04-08 18:15:43 class Solution: def integerBreak(self, n: int) -> int: # 前 5 個數的最大乘積 tmp = [1, 2, 4, 6, 9] for i in range(5, n - 1): # 動態規劃:第 i 個數 的最大乘積爲 3 * 往前數第三個數 tmp.append(3 * tmp[i - 3]) return tmp[n - 2] def integerBreak2(self, n: int) -> int: # 思路與上面的思路一致,優化了空間 if n == 2: return 1 if n == 3: return 2 tmp = [4, 6, 9] for i in range(n - 6): tmp[i % 3] *= 3 return tmp[(n - 1) % 3]