-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy path1298.cpp
More file actions
39 lines (36 loc) · 1.18 KB
/
1298.cpp
File metadata and controls
39 lines (36 loc) · 1.18 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
class Solution {
public:
int maxCandies(vector<int>& status, vector<int>& candies, vector<vector<int>>& keys, vector<vector<int>>& containedBoxes, vector<int>& initialBoxes) {
int n = status.size();
vector<bool> visited(n, false);
queue<int> q;
unordered_set<int> waiting;
for (auto& box : initialBoxes) {
if (status[box]) q.push(box);
else waiting.insert(box);
}
int res = 0 ;
while (!q.empty()) {
int n = q.size();
while (n--) {
int box = q.front();
q.pop();
res += candies[box];
visited[box] = true;
for (auto& key : keys[box]) {
if (status[key] == 0 && waiting.count(key)) {
q.push(key);
}
status[key] = 1;
}
for (auto& bbox : containedBoxes[box]) {
if (status[bbox]) {
if (!visited[bbox]) q.push(bbox);
}
else waiting.insert(bbox);
}
}
}
return res;
}
};