0279. Perfect Squares
class Solution {
public:
int numSquares(int n) {
// dp[i] = least number of perfect square numbers
vector<int> dp(n+1, 10001);
dp[0] = 0;
for(int i = 0; i <= n; i++) {
for(int j = 1; j * j <= i; j++) {
dp[i] = min(dp[i], dp[i - j*j] + 1);
}
}
return dp[n] == 10001 ? 0 : dp[n];
}
};