-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSolution.cs
More file actions
40 lines (37 loc) · 1.09 KB
/
Solution.cs
File metadata and controls
40 lines (37 loc) · 1.09 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
public class Solution
{
public int MaxCandies(int[] status, int[] candies, int[][] keys, int[][] containedBoxes, int[] initialBoxes)
{
int n = status.Length;
int ret = 0;
int[] state = new int[n];
bool[] hasKeys = new bool[n];
Queue<int> queue = [];
foreach (int box in initialBoxes)
{
queue.Enqueue(box);
}
while (queue.Count > 0)
{
int curr = queue.Dequeue();
if (state[curr] == 2) continue;
state[curr] = 1;
if (hasKeys[curr] || status[curr] == 1)
{
hasKeys[curr] = true;
state[curr] = 2;
ret += candies[curr];
foreach (int box in keys[curr])
{
hasKeys[box] = true;
if (state[box] == 1) queue.Enqueue(box);
}
foreach (int next in containedBoxes[curr])
{
if (state[next] == 0) queue.Enqueue(next);
}
}
}
return ret;
}
}