# leetcode 343. Integer Break（从递归到动态规划）

Input: 2
Output: 1
Explanation: 2 = 1 + 1, 1 × 1 = 1.

public int integerBreak(int n) {
int max = -1;
for(int i=1;i<=n-1;i++){
max = max(max,i*(n-i),i*integerBreak(n-i));
}
return max;

}
int max(int a,int b,int c){
return Math.max(Math.max(a, b), c);
}


	int[] memo;
public int integerBreak(int n) {
memo = new int[n+1];
for(int i=0;i<=n;i++)
memo[i] = -1;
return integerBreakson(n);
}
public int integerBreakson(int n){
int max = -1;
if(memo[n]!=-1)
return memo[n];

for(int i=1;i<=n-1;i++){
max =  max(max,i*(n-i),i*integerBreakson(n-i));
}
memo[n] = max;
return max;
}
int max(int a,int b,int c){
return Math.max(Math.max(a, b), c);
}


 static int  max(int a,int b,int c){
return Math.max(Math.max(a, b), c);
}

//leetcode 343. Integer Break   自下向上，动态规划
static int[] memo;
public static int integerBreak(int n) {
memo = new int[n+1];
for(int i=0;i<=n;i++)
memo[i] = -1;
memo[1] = 1;
for (int i = 2; i <= n; i++) {
int max2 = -1;
for (int j = 1; j <= i - 1; j++) {
max2 = max(max2, j * (i - j), j * memo[i - j]);
}
memo[i] = max2;

}
return memo[n];
}