3
int[] status, int[] candies, int[][] keys, int[][] containedBoxes, int[] initialBoxes) {4
int n = initialBoxes.length;8
Queue<Integer> q = new LinkedList<>();11
for (int i : initialBoxes) {15
totalCandies += candies[i]; // Add all Candies of intial box;16
for (int j = 0; j < containedBoxes[i].length; j++) {17
q.add(containedBoxes[i][j]); // Add all Contained Boxes in queue;19
for (int j = 0; j < keys[i].length; j++) {20
status[keys[i][j]] = 1; // Set status of of box=1 if we found key;25
while (q.size() > 0 && isValid(q, status)) {28
totalCandies += candies[b]; // Add all Candies of selected box;30
for (int j = 0; j < containedBoxes[b].length; j++) {31
q.add(containedBoxes[b][j]); // Add all Contained Boxes in queue;33
for (int j = 0; j < keys[b].length; j++) {34
status[keys[b][j]] = 1; // Set status of of box=1 if we found key;36
} else q.add(b); // If Status==0 add again in queue;41
/* This function helps to know whether any boxes in queue status is still open or not42
if all boxes status is off then it will return false and above while loop while terminate beacue now we cannot get any candy*/43
public boolean isValid(Queue<Integer> q, int[] status) {44
Queue<Integer> cq = new LinkedList<>(q);46
while (cq.size() > 0) {47
if (status[cq.poll()] == 1) return true;