题解 | #剪绳子#
剪绳子
http://www.nowcoder.com/practice/57d85990ba5b440ab888fc72b0751bf8
参考大佬的动态规划算法,从下向上计算
public class Solution {
public int cutRope(int target) {
if(target<2)
return 0;
if(target == 2)
return 1;
if(target == 3)
return 2;
int products[] = new int[target+1];
products[0] = 0;
products[1] = 1;
products[2] = 2;
products[3] = 3;
int ans = 0;
for(int i=4;i<target+1;i++){
for(int j=1;j<i/2+1;j++){
ans = Math.max(products[j]*products[i-j], ans);
}
products[i] = ans;
}
ans = products[target];
return ans;
}
}