343 Integer Break
343. Integer Break
1. Question
Given a positive integern, 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, givenn= 2, return 1 (2 = 1 + 1); givenn= 10, return 36 (10 = 3 + 3 + 4).
Note: You may assume thatnis not less than 2 and not larger than 58.
2. Implementation
(1) DP
思路: dp[i]表示i能分解成的integer后的最大乘积, 对于每个整数i,我们再1 - i的范围内找到一个整数j, 使得dp[i] = Math.max(dp[i], j * Math.max(i - j, dp[i - j]);
3. Time & Space Complexity
DP: 时间复杂度O(n^2), 空间复杂度O(n)
Last updated
Was this helpful?