-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathIntegerBreak.cpp
More file actions
39 lines (35 loc) · 841 Bytes
/
Copy pathIntegerBreak.cpp
File metadata and controls
39 lines (35 loc) · 841 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
/*
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.
Input: 2
Output: 1
Input: 10
Output: 36
*/
class Solution {
public:
int calcans(int *dp,int i,int n)
{
if(dp[i]!=-1)
return dp[i];
int pr=1;
int f=i==n?i-1:i;
for(int j=1;j<=f;j++)
{
int max_a=j*calcans(dp,i-j,n);
if(max_a>=pr)
pr=max_a;
}
dp[i]=pr;
return pr;
}
int integerBreak(int n) {
int *dp=(int *)malloc((n+1)*(sizeof(int)));
for(int i=1;i<=n;i++)
dp[i]=-1;
dp[0]=1;
int ans=calcans(dp,n,n);
free(dp);
return ans;
}
};