forked from cheungYX/algorithm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPerfectSquares.java
71 lines (65 loc) · 1.66 KB
/
PerfectSquares.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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
// version 0 DP
public class Solution {
/**
* @param n a positive integer
* @return an integer
*/
public int numSquares(int n) {
// Write your code here
int[] dp = new int[n + 1];
Arrays.fill(dp, Integer.MAX_VALUE);
for(int i = 0; i * i <= n; ++i) {
dp[i * i] = 1;
}
for (int i = 0; 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];
}
}
// version 1 DP
public class Solution {
/**
* @param n a positive integer
* @return an integer
*/
public int numSquares(int n) {
// Write your code here
int[] dp = new int[n + 1];
Arrays.fill(dp, Integer.MAX_VALUE);
for(int i = 0; i * i <= n; ++i)
dp[i * i] = 1;
for (int i = 0; i <= n; ++i)
for (int j = 0; i + j * j <= n; ++j)
dp[i + j * j] = Math.min(dp[i] + 1, dp[i + j * j]);
return dp[n];
}
}
// version 2 Math
public class Solution {
/**
* @param n a positive integer
* @return an integer
*/
public int numSquares(int n) {
// Write your code here
while (n % 4 == 0)
n /= 4;
if (n % 8 == 7)
return 4;
for (int i = 0; i * i <= n; ++i) {
int j = (int)Math.sqrt(n * 1.0 - i * i);
if (i * i + j * j == n) {
int res = 0;
if (i > 0)
res += 1;
if (j > 0)
res += 1;
return res;
}
}
return 3;
}
}