题意:给出一个整数,求其等于若干个平方数的和的最小值
思路:用动态规划
具体代码如下:
public class Solution
{
public int numSquares(int n)
{
int[] f = new int[n + 1];
f[0] = 0;
for (int i = 1; i <= n; i++)
{
f[i] = Integer.MAX_VALUE;
int m = (int)Math.sqrt(i);
for (int j = 1; j <= m; j++)
{
f[i] = Math.min(f[i], 1 + f[i - j * j]);
}
}
return f[n];
}
}