Leetcode - 343 Integer Break (Medium)
题目描述:给定一个整数 n,求相加等于 n 并且成绩最大的一组整数的积。
Input: 10
Output: 36
Explanation: 10 = 3 + 3 + 4, 3 × 3 × 4 = 36.
解题思路:定义状态 dp[i] 为 i 对应的最大乘积。
public int integerBreak(int n) {
int[] dp = new int[n + 1];
dp[1] = 1;
for(int i = 2; i <= n; i++){
for(int j = 1; j <= i - 1; j++){
dp[i] = Math.max(dp[i], Math.max(dp[i - j] * j, (i - j) * j));
}
}
return dp[n];
}