Skip to content

Commit f6df1c1

Browse files
committed
Sync LeetCode submission Runtime - 8 ms (96.55%), Memory - 28.1 MB (65.13%)
1 parent 77490ff commit f6df1c1

File tree

2 files changed

+92
-0
lines changed

2 files changed

+92
-0
lines changed
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
<p>You have <code>n</code> boxes labeled from <code>0</code> to <code>n - 1</code>. You are given four arrays: <code>status</code>, <code>candies</code>, <code>keys</code>, and <code>containedBoxes</code> where:</p>
2+
3+
<ul>
4+
<li><code>status[i]</code> is <code>1</code> if the <code>i<sup>th</sup></code> box is open and <code>0</code> if the <code>i<sup>th</sup></code> box is closed,</li>
5+
<li><code>candies[i]</code> is the number of candies in the <code>i<sup>th</sup></code> box,</li>
6+
<li><code>keys[i]</code> is a list of the labels of the boxes you can open after opening the <code>i<sup>th</sup></code> box.</li>
7+
<li><code>containedBoxes[i]</code> is a list of the boxes you found inside the <code>i<sup>th</sup></code> box.</li>
8+
</ul>
9+
10+
<p>You are given an integer array <code>initialBoxes</code> that contains the labels of the boxes you initially have. You can take all the candies in <strong>any open box</strong> and you can use the keys in it to open new boxes and you also can use the boxes you find in it.</p>
11+
12+
<p>Return <em>the maximum number of candies you can get following the rules above</em>.</p>
13+
14+
<p>&nbsp;</p>
15+
<p><strong class="example">Example 1:</strong></p>
16+
17+
<pre>
18+
<strong>Input:</strong> status = [1,0,1,0], candies = [7,5,4,100], keys = [[],[],[1],[]], containedBoxes = [[1,2],[3],[],[]], initialBoxes = [0]
19+
<strong>Output:</strong> 16
20+
<strong>Explanation:</strong> You will be initially given box 0. You will find 7 candies in it and boxes 1 and 2.
21+
Box 1 is closed and you do not have a key for it so you will open box 2. You will find 4 candies and a key to box 1 in box 2.
22+
In box 1, you will find 5 candies and box 3 but you will not find a key to box 3 so box 3 will remain closed.
23+
Total number of candies collected = 7 + 4 + 5 = 16 candy.
24+
</pre>
25+
26+
<p><strong class="example">Example 2:</strong></p>
27+
28+
<pre>
29+
<strong>Input:</strong> status = [1,0,0,0,0,0], candies = [1,1,1,1,1,1], keys = [[1,2,3,4,5],[],[],[],[],[]], containedBoxes = [[1,2,3,4,5],[],[],[],[],[]], initialBoxes = [0]
30+
<strong>Output:</strong> 6
31+
<strong>Explanation:</strong> You have initially box 0. Opening it you can find boxes 1,2,3,4 and 5 and their keys.
32+
The total number of candies will be 6.
33+
</pre>
34+
35+
<p>&nbsp;</p>
36+
<p><strong>Constraints:</strong></p>
37+
38+
<ul>
39+
<li><code>n == status.length == candies.length == keys.length == containedBoxes.length</code></li>
40+
<li><code>1 &lt;= n &lt;= 1000</code></li>
41+
<li><code>status[i]</code> is either <code>0</code> or <code>1</code>.</li>
42+
<li><code>1 &lt;= candies[i] &lt;= 1000</code></li>
43+
<li><code>0 &lt;= keys[i].length &lt;= n</code></li>
44+
<li><code>0 &lt;= keys[i][j] &lt; n</code></li>
45+
<li>All values of <code>keys[i]</code> are <strong>unique</strong>.</li>
46+
<li><code>0 &lt;= containedBoxes[i].length &lt;= n</code></li>
47+
<li><code>0 &lt;= containedBoxes[i][j] &lt; n</code></li>
48+
<li>All values of <code>containedBoxes[i]</code> are unique.</li>
49+
<li>Each box is contained in one box at most.</li>
50+
<li><code>0 &lt;= initialBoxes.length &lt;= n</code></li>
51+
<li><code>0 &lt;= initialBoxes[i] &lt; n</code></li>
52+
</ul>
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
# Approach: Breadth-First Search
2+
3+
# Time: O(n)
4+
# Space: O(n)
5+
6+
from collections import deque
7+
8+
class Solution:
9+
def maxCandies(self, status: List[int], candies: List[int], keys: List[List[int]], containedBoxes: List[List[int]], initialBoxes: List[int]) -> int:
10+
n = len(status)
11+
can_open = [status[i] == 1 for i in range(n)]
12+
has_box, used = [False] * n, [False] * n
13+
14+
q = deque()
15+
ans = 0
16+
17+
for box in initialBoxes:
18+
has_box[box] = True
19+
if can_open[box]:
20+
q.append(box)
21+
used[box] = True
22+
ans += candies[box]
23+
24+
while len(q) > 0:
25+
big_box = q.popleft()
26+
for key in keys[big_box]:
27+
can_open[key] = True
28+
if not used[key] and has_box[key]:
29+
q.append(key)
30+
used[key] = True
31+
ans += candies[key]
32+
for box in containedBoxes[big_box]:
33+
has_box[box] = True
34+
if not used[box] and can_open[box]:
35+
q.append(box)
36+
used[box] = True
37+
ans += candies[box]
38+
39+
return ans
40+

0 commit comments

Comments
 (0)