leetcode-343 Integer Break

Posted on 2020-11-01 21:29  actor_spider  阅读(56)  评论(0)    收藏  举报
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.
Input: 2
Output: 1
Explanation: 2 = 1 + 1, 1 × 1 = 1.

class Solution: def integerBreak(self, n: int) -> int: if n == 2: return 1 elif n == 3: return 2 else: sum1 = 1 while n > 4: sum1 *= 3 n -= 3 sum1 *=n return sum1

  这道题方法很多,最简单的就是找规律,当然也可以采用dp来做。

博客园  ©  2004-2026
浙公网安备 33010602011771号 浙ICP备2021040463号-3