forked from EpicDevClub/excel-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInteger Break.java
More file actions
31 lines (30 loc) · 829 Bytes
/
Integer Break.java
File metadata and controls
31 lines (30 loc) · 829 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
//Integer Break is a problem on Leetcode in the Dynamic Programming ThreadGroup
//Problem URL: https://leetcode.com/problems/integer-break/
//It is a very good problem and my solution here is faster than 100% submissions
class Solution {
public:
int integerBreak(int n) {
if(n==2)
return 1;
if(n==3)
return 2;
int i,j;
int dp[n+1][n+1];
for(i=0;i<=n;i++)
{
dp[i][0]=0;dp[0][i]=1;
// dp[i][1]=1;dp[1][i]=1;
}
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
{
if(j>i)
dp[i][j]=dp[i][j-1];
else
dp[i][j]=max(dp[i][j-1],dp[i-j][j]*j);
}
}
return dp[n][n];
}
};