Maximum Candies You Can Get from Boxes LeetCode solution in Python

1
class Solution:
2
count = 0 # Found candy. Class variable to keep found candy
3

4
def maxCandies(
5
self,
6
status: List[int],
7
candies: List[int],
8
keys: List[List[int]],
9
containedBoxes: List[List[int]],
10
initialBoxes: List[int],
11
) -> int:
12
boxes = [] # Newly found boxes
13
progress = False
14
for box in initialBoxes:
15
if status[box]: # The box is open
16
progress = True
17
boxes.extend(containedBoxes[box]) # Add newly found boxes
18
self.count += candies[box] # Count found candy
19
for key in keys[box]:
20
status[key] = (
21
1 # Use found keys to open boxes even if we don't have them.
22
)
23
else:
24
boxes.append(box) # The box is closed. Keep it for the next iteration.
25
if not progress: # Nothing happened. return.
26
return self.count
27
# Run another iteration with the new 'boxes'
28
return self.maxCandies(status, candies, keys, containedBoxes, boxes)

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0