-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path279.Perfect_Squares(M).cpp
47 lines (44 loc) · 1.19 KB
/
279.Perfect_Squares(M).cpp
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
class Solution {
public:
vector<int> generateSquares(int n){
vector<int> squares;
int square = 1;
int diff = 3;
while(square<=n){
squares.push_back(square);
square += diff;
diff += 2; // 1 4 9 16 平方数产生规律
}
return squares;
}
int numSquares(int n) {
vector<int>squares = generateSquares(n);
queue<int>q;
bool marked[n+1];
for(int i = 0; i < n + 1; ++i)
marked[i] = false;
q.push(n);
marked[n] = true;
int level = 0;
while (!q.empty()){
int size = q.size();
level++;
while (size-- > 0){
int cur = q.front();
q.pop();
for (auto s: squares){
int next = cur - s;
if (next < 0)
break;
if (next == 0)
return level;
if (marked[next])
continue;
marked[next] = true;
q.push(next);
}
}
}
return n;
}
};