Skip to content

Commit 9055800

Browse files
committed
Jun 03
1 parent 6c641e7 commit 9055800

File tree

2 files changed

+55
-2
lines changed

2 files changed

+55
-2
lines changed
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
from collections import deque
2+
3+
4+
class Solution:
5+
def maxCandies(
6+
self,
7+
status: list[int],
8+
candies: list[int],
9+
keys: list[list[int]],
10+
containedBoxes: list[list[int]],
11+
initialBoxes: list[int]) -> int:
12+
queue = deque([])
13+
# store flags if reachable but unopened
14+
boxes = [False] * len(status)
15+
for box in initialBoxes:
16+
if status[box] == 1:
17+
queue.append(box)
18+
else:
19+
boxes[box] = True
20+
count = 0
21+
while queue:
22+
box = queue.popleft()
23+
count += candies[box]
24+
for i in keys[box]:
25+
if status[i] == 0 and boxes[i] is True:
26+
queue.append(i)
27+
status[i] = 1
28+
for i in containedBoxes[box]:
29+
if status[i] == 1:
30+
queue.append(i)
31+
else:
32+
boxes[i] = True
33+
return count
34+
35+
36+
def main():
37+
status = [1, 0, 1, 0]
38+
candies = [7, 5, 4, 100]
39+
keys = [[], [], [1], []]
40+
containedBoxes = [[1, 2], [3], [], []]
41+
initialBoxes = [0]
42+
assert Solution().maxCandies(status, candies, keys, containedBoxes, initialBoxes) == 16
43+
44+
status = [1, 0, 0, 0, 0, 0]
45+
candies = [1, 1, 1, 1, 1, 1]
46+
keys = [[1, 2, 3, 4, 5], [], [], [], [], []]
47+
containedBoxes = [[1, 2, 3, 4, 5], [], [], [], [], []]
48+
initialBoxes = [0]
49+
assert Solution().maxCandies(status, candies, keys, containedBoxes, initialBoxes) == 6
50+
51+
52+
if __name__ == '__main__':
53+
main()

2025-06-June-LeetCoding-Challenge/README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
| --- | --- | --- | --- |
77
| June 01 | [2929. Distribute Candies Among Children II](https://leetcode.com/problems/distribute-candies-among-children-ii/) | Medium | Unsolved |
88
| June 02 | [135. Candy](https://leetcode.com/problems/candy/) | Hard | Solved |
9-
| June 03 | []() | | |
9+
| June 03 | [1298. Maximum Candies You Can Get from Boxes](https://leetcode.com/problems/maximum-candies-you-can-get-from-boxes/) | Hard | Solved |
1010
| June 04 | []() | | |
1111
| June 05 | []() | | |
1212
| June 06 | []() | | |
@@ -41,4 +41,4 @@
4141
| --- | --- | --- | --- |
4242
| Easy | 0 | 0 | 0 |
4343
| Medium | 1 | 0 | 1 |
44-
| Hard | 1 | 1 | 0 |
44+
| Hard | 2 | 2 | 0 |

0 commit comments

Comments
 (0)