forked from fishercoder1534/Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
_279.java
40 lines (36 loc) · 1.04 KB
/
_279.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
package com.fishercoder.solutions;
import java.util.Arrays;
public class _279 {
public static class Solution1 {
public int numSquares(int n) {
int result = n;
int num = 2;
while (num * num <= n) {
int temp1 = n / (num * num);
int temp2 = n % (num * num);
result = Math.min(result, temp1 + numSquares(temp2));
num++;
}
return result;
}
}
public static class Solution2 {
//DP solution
public int numSquares(int n) {
int[] dp = new int[n + 1];
Arrays.fill(dp, Integer.MAX_VALUE);
dp[0] = 0;
dp[1] = 1;
for (int i = 1; i <= n; i++) {
int min = Integer.MAX_VALUE;
int j = 1;
while (i - j * j >= 0) {
min = Math.min(min, dp[i - j * j] + 1);
j++;
}
dp[i] = min;
}
return dp[n];
}
}
}