forked from luliyucoordinate/Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1298.java
More file actions
22 lines (21 loc) · 767 Bytes
/
Copy path1298.java
File metadata and controls
22 lines (21 loc) · 767 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
class Solution {
public int maxCandies(int[] status, int[] candies, int[][] keys, int[][] containedBoxes, int[] initialBoxes) {
Queue<Integer> q = new LinkedList();
for (int i : initialBoxes) q.offer(i);
int res = 0;
while (!q.isEmpty()) {
Boolean changed = false;
for (int q_len = q.size(); q_len > 0; q_len--) {
int b = q.poll();
if (status[b] == 1) {
changed = true;
res += candies[b];
for (int i : containedBoxes[b]) q.offer(i);
for (int i : keys[b]) status[i] = 1;
} else q.offer(b);
}
if (!changed) return res;
}
return res;
}
}