-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path343. Integer Break.cpp
More file actions
46 lines (41 loc) · 1012 Bytes
/
343. Integer Break.cpp
File metadata and controls
46 lines (41 loc) · 1012 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
40
41
42
43
44
45
46
class Solution {
public:
int integerBreak(int n) {
if(n == 2)
return 1;
else if(n == 3)
return 2;
else if(n%3 == 0)
return pow(3, n/3);
else if(n%3 == 1)
return 2 * 2 * pow(3, (n - 4) / 3);
else
return 2 * pow(3, n/3);
}
};
/*second approach*/
class Solution {
public:
int integerBreak(int n) {
if(n == 2) return 1;
if(n == 3) return 2;
if(n == 4) return 4;
if(n == 5) return 6;
if(n == 6) return 9;
int *dp = (int *)malloc(sizeof(int) * (n + 1));
dp[2] = 1,dp[3] = 2,dp[4] = 4,dp[5] = 6,dp[6] = 9;
for(int i=7;i<=n;i++){
dp[i] = 2 * dp[i-2] > 3 * dp[i-3] ? 2 * dp[i-2] : 3 * dp[i-3];
}
int ans = dp[n];
free(dp);
return ans;
}
};
int main(){
Solution solve;
for(int i=3;i<59;i++){
cout<<i<<" : "<<solve.integerBreak(i)<<endl;
}
return 0;
}