Skip to content

Commit 6adf016

Browse files
authored
Create maximum-candies-you-can-get-from-boxes.py
1 parent 85d7a8a commit 6adf016

File tree

1 file changed

+34
-0
lines changed

1 file changed

+34
-0
lines changed
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
# Time: O(n^2)
2+
# Space: O(n)
3+
4+
import collections
5+
6+
7+
class Solution(object):
8+
def maxCandies(self, status, candies, keys, containedBoxes, initialBoxes):
9+
"""
10+
:type status: List[int]
11+
:type candies: List[int]
12+
:type keys: List[List[int]]
13+
:type containedBoxes: List[List[int]]
14+
:type initialBoxes: List[int]
15+
:rtype: int
16+
"""
17+
result = 0
18+
q = collections.deque(initialBoxes)
19+
while q:
20+
changed = False
21+
for _ in xrange(len(q)):
22+
box = q.popleft()
23+
if not status[box]:
24+
q.append(box)
25+
continue
26+
changed = True
27+
result += candies[box]
28+
for contained_key in keys[box]:
29+
status[contained_key] = 1
30+
for contained_box in containedBoxes[box]:
31+
q.append(contained_box)
32+
if not changed:
33+
break
34+
return result

0 commit comments

Comments
 (0)