LeetCode 279.
Tags: LeetCode
Complexity
-
Time complexity: O(nsqrt(n))*
-
Space complexity: O(n)
Code
class Solution {
public int numSquares(int n) {
int[] dp = new int[10001];
Arrays.fill(dp, 10000);
dp[0] = 0;
for (int i=1; i<=n; i++) {
for (int j=1; j*j <= i; j++) {
dp[i] = Math.min(dp[i], dp[i - j*j] + 1);
}
}
return dp[n];
}
}
Check out the description of this problem at LC 279.