public class Solution {
public int numSquares(int n) {
int[] dp = new int[n + 1];
dp[0] = 0;
for (int i = 1; i <= n; i++) {
int min = Integer.MAX_VALUE;
int sqrt = (int)Math.sqrt(i);
int j = 1;
while (j <= sqrt) {
min = Math.min(min, dp[i - j * j] + 1);
j++;
}
dp[i] = min;
}
return dp[n];
}
}
LeetCode 279. Perfect Squares
最新推荐文章于 2023-02-10 23:09:56 发布