class Solution {
public:
int numSquares(int n) {
int * dp = new int[max(n + 1, 3)];
dp[0] = 0;
dp[1] = 1;
dp[2] = 2;
for (int i = 3; i <= n; i++)
{
int min = INT_MAX;
for (int j = sqrt(i); j >= 1; j--)
{
int mm = dp[i - j * j] + 1;
if (min > mm)
{
min = mm;
}
}
dp[i] = min;
}
return dp[n];
}
};
public:
int numSquares(int n) {
int * dp = new int[max(n + 1, 3)];
dp[0] = 0;
dp[1] = 1;
dp[2] = 2;
for (int i = 3; i <= n; i++)
{
int min = INT_MAX;
for (int j = sqrt(i); j >= 1; j--)
{
int mm = dp[i - j * j] + 1;
if (min > mm)
{
min = mm;
}
}
dp[i] = min;
}
return dp[n];
}
};
评论
发表评论