343. Integer Break
Input: 2
Output: 1
Explanation: 2 = 1 + 1, 1 × 1 = 1.Input: 10
Output: 36
Explanation: 10 = 3 + 3 + 4, 3 × 3 × 4 = 36.// Brute Force Dynamic Programming
int integerBreak(int n) { // time: O(n^2); space: O(n)
vector<int> dp(n + 1, 0);
dp[1] = 1;
for (int i = 2; i <= n; ++i) {
for (int j = 1; j <= i / 2; ++j) {
dp[i] = max(dp[i], max(j, dp[j]) * max(i - j, dp[i - j]));
}
}
return dp.back();
}Last updated