class Solution {
public:
int numSquares(int n) {
// 下标 dp[j] 和为j的最少完全平方数个数
// 初始化 0
// dp[j] = min(dp[j - i * i] + 1, dp[j])
// 顺序无关
vector<int> dp(n + 1, INT_MAX);
// !!!
dp[0] = 0;
for (int i = 0; i * i <= n; i++) {
for (int j = i * i; j <= n; j++) {
if (dp[j - i * i] != INT_MAX)
dp [j] = min(dp[j - i * i] + 1, dp[j]);
}
}
return dp[n];
}
};
03-29
233
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)
04-24
145
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)