-
Notifications
You must be signed in to change notification settings - Fork 119
/
Copy pathMaximum Candies You Can Get from Boxes.java
70 lines (61 loc) · 1.93 KB
/
Maximum Candies You Can Get from Boxes.java
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
// Runtime: 6 ms (Top 61.19%) | Memory: 57.10 MB (Top 59.7%)
class Solution {
public int maxCandies(int[] status, int[] candies, int[][] keys, int[][] containedBoxes, int[] initialBoxes)
{
int n=initialBoxes.length;
if(n==0)
return 0;
Queue<Integer> q=new LinkedList<>();
int totalCandies=0;
for(int i:initialBoxes)
{
if(status[i]==0)
{
q.add(i);
}
else
{
totalCandies+=candies[i]; // Add all Candies of intial box;
for(int j=0;j<containedBoxes[i].length;j++)
{
q.add(containedBoxes[i][j]); // Add all Contained Boxes in queue;
}
for(int j=0;j<keys[i].length;j++)
{
status[keys[i][j]]=1; // Set status of of box=1 if we found key;
}
}
}
while(q.size()>0 && isValid(q,status))
{
int b=q.poll();
if(status[b]==1)
{
totalCandies+=candies[b]; // Add all Candies of selected box;
for(int j=0;j<containedBoxes[b].length;j++)
{
q.add(containedBoxes[b][j]); // Add all Contained Boxes in queue;
}
for(int j=0;j<keys[b].length;j++)
{
status[keys[b][j]]=1; // Set status of of box=1 if we found key;
}
}
else
q.add(b); // If Status==0 add again in queue;
}
return totalCandies;
}
/* This function helps to know whether any boxes in queue status is still open or not
if all boxes status is off then it will return false and above while loop while terminate beacue now we cannot get any candy*/
public boolean isValid(Queue<Integer> q,int[] status)
{
Queue<Integer> cq=new LinkedList<>(q);
while(cq.size()>0)
{
if(status[cq.poll()]==1)
return true;
}
return false;
}
}