Problem
Given an integern, break it into the sum of k positive integers, where k >= 2, and maximize the product of those integers.
Return the maximum product you can get.
Examples
Constraints
- 2 <= n <= 58
Documentation Index
Fetch the complete documentation index at: /llms.txt
Use this file to discover all available pages before exploring further.
Tested Python solution for LeetCode 343 with 15 pytest cases. Generate a practice environment with lcpy.
lcpy gen -n 343 # by problem number
lcpy gen -s integer_break # by problem name
n, break it into the sum of k positive integers, where k >= 2, and maximize the product of those integers.
Return the maximum product you can get.
Input: n = 2
Output: 1
Explanation: 2 = 1 + 1, 1 * 1 = 1.
Input: n = 10
Output: 36
Explanation: 10 = 3 + 3 + 4, 3 * 3 * 4 = 36.
class Solution:
# Time: O(n^2)
# Space: O(n)
def integer_break(self, n: int) -> int:
dp = [0] * (n + 1)
dp[1] = 1
for target in range(2, n + 1):
for part in range(1, target):
dp[target] = max(dp[target], part * (target - part), part * dp[target - part])
return dp[n]
| Time | Space |
|---|---|
| O(n^2) | O(n) |