-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSolution.java
More file actions
47 lines (44 loc) · 1.27 KB
/
Solution.java
File metadata and controls
47 lines (44 loc) · 1.27 KB
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
package perfectSquares;
import java.util.ArrayList;
import java.util.List;
public class Solution {
private List<Integer> getSquares(int n) {
int a = 1;
int diff = 3;
List<Integer> squares = new ArrayList<>();
while (a <= n) {
squares.add(a);
a += diff;
diff += 2;
}
return squares;
}
public int numSquares(int n) {
List<Integer> squares = getSquares(n);
int[] visited = new int[n + 1];
List<Integer> queue = new ArrayList<>();
queue.add(n);
visited[n] = 1;
int count = 0;
while (!queue.isEmpty()) {
int qSize = queue.size();
count++;
while (qSize-- > 0) {
int x = queue.get(0);
queue.remove(0);
for (int i = 0; i < squares.size() && squares.get(i) <= x; i++) {
int next = x - squares.get(i);
if (visited[next] == 1) {
continue;
}
if (next == 0) {
return count;
}
queue.add(next);
visited[next] = 1;
}
}
}
return count;
}
}