分析:从1到n,记录每个int值i的最小被其之前元素求和的值
代码:
public int Solution(int n) {
int[] res = new int[n+1];
res[1] = 1;
for(int i=2;i<=n;i++){
int min = Integer.MAX_VALUE;
int j = 1;
while(j*j<=i) {
if(j*j == i){
min = 1;
break;
}
min = Math.min(min, res[j*j]+res[i-j*j]);
++j;
}
res[i] = min;
}
return res[n];
}