C++
int integerBreak(int n) {
vector<int> dp={0,0,1,2,4,6,9};
for(int i=7;i<=n;i++)
{
dp.push_back(3*dp[i-3]);
}
return dp[n];
}
Java
public int integerBreak(int n) {
if (n == 2)
return 1;
if (n == 3)
return 2;
int[] dp = new int[n+1];
dp[2] = 2;
dp[3] = 3;
dp[4] = 4;
for (int i = 5; i <= n; i++) {
dp[i] = dp[i-3] * 3;
}
return dp[n];
}
网友评论