タイトル
https://leetcode-cn.com/problems/jian-sheng-zi-lcof/
アイデア
f(n)= f(ni)* f(i)…
長さが1 2 3の場合、計算に参加する必要はありません
。各セグメントの最大値は上から下に計算できます。
class Solution {
public int cuttingRope(int length) {
if(length<2){
return 0;
}
if(length==2){
return 1;
}
if(length==3){
return 2;
}
int[] product = new int[length+1];
product[0] = 0;
product[1] = 1;
product[2] = 2;
product[3] = 3;
int max = 0;
for (int i = 4; i <= length; i++) {
max =0;
for (int j = 1; j <= i / 2; j++) {
int temp = product[j]*product[i-j];
if(max<temp){
max = temp;
}
product[i] = max;
}
}
return product[length];
}
}