-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsolution.cpp
More file actions
64 lines (59 loc) · 1.16 KB
/
solution.cpp
File metadata and controls
64 lines (59 loc) · 1.16 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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
//
// Created by mingyi on 24.04.20.
//
#include <vector>
#include <queue>
#include <cassert>
using namespace std;
vector<int> squares(int n) {
int current = 1;
int diff = 3;
vector<int> sq;
while (current <= n) {
sq.push_back(current);
current += diff;
diff += 2;
}
return sq;
}
int numSquaresBFS(int n) {
vector<int> sq = squares(n);
vector<bool> visited(n + 1, false);
queue<int> q;
q.push(n);
visited[n] = true;
int count = 0;
while (!q.empty()) {
count++;
int qSize = q.size();
while (qSize--) {
int cur = q.front();
q.pop();
for (int s : sq) {
int next = cur - s;
if (next < 0) {
break;
}
if (next == 0) {
return count;
}
if (visited[next]) {
continue;
}
visited[next] = true;
q.push(next);
}
}
}
return count;
}
int main() {
vector<int> expectSquares{1, 4, 9, 16};
vector<int> resultSquares = squares(20);
for (int i = 0; i < resultSquares.size(); i++) {
assert(expectSquares[i] == resultSquares[i]);
}
assert(3 == numSquaresBFS(12));
assert(2 == numSquaresBFS(13));
return 0;
}