- Integer Splitting
Given a positive integer n, split it into the sum of k positive integers (k >= 2) and maximize the product of these integers.
Returns the largest product you can get.
class Solution {
public int integerBreak(int n) {
int[] dp = new int[n + 1];//正整数,根据dp数组的定义,从1到n,加上0位置,共n + 1个位置
dp[2] = 1;
for (int i = 3; i <= n; i++) {
for (int j = 0; j <= i - j; j++) {
dp[i] = Math.max(dp[i], Math.max(j * (i - j), j * dp[i - j]));//累计比较拆分为2个,多个数的最大乘积
}
}
return dp[n];
}
}