Given a positive integer n, 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, given n = 2, return 1 (2 = 1 + 1); given n = 10, return 36 (10 = 3 + 3 + 4).
Note: You may assume that n is not less than 2 and not larger than 58.
做的时候按照dp来做的
class Solution {
public:
int integerBreak(int n) {
vector<int> dp(60,0);
dp[1]=1;
dp[2]=1;
for(int i=3;i<=58;i++){
dp[i]=i-1;
for(int j=1;j<i;j++){
int a=j,b=i-j;
int temp=max(a*b,max(a*dp[b],max(dp[a]*b,dp[a]*dp[b])));
if(dp[i]<temp) dp[i]=temp;
}
}
return dp[n];
}
};
学习别人的博客才知道可以按照数学来做,尽可能看其中3的个数
解决一道题的思路千千万~