1298. Maximum Candies You Can Get from Boxes
Read the full problem statement on LeetCode.
Difficulty: hard Acceptance: 57% Topics: Array, Breadth-First Search, Graph
View full problem on LeetCode Reading material
Reference solution (spoiler · python)
# Time: O(n^2)
# Space: O(n)
import collections
class Solution(object):
def maxCandies(self, status, candies, keys, containedBoxes, initialBoxes):
"""
:type status: List[int]
:type candies: List[int]
:type keys: List[List[int]]
:type containedBoxes: List[List[int]]
:type initialBoxes: List[int]
:rtype: int
"""
result = 0
q = collections.deque(initialBoxes)
while q:
changed = False
for _ in xrange(len(q)):
box = q.popleft()
if not status[box]:
q.append(box)
continue
changed = True
result += candies[box]
for contained_key in keys[box]:
status[contained_key] = 1
for contained_box in containedBoxes[box]:
q.append(contained_box)
if not changed:
break
return result
Solution from kamyu104/LeetCode-Solutions · MIT