Maximum Candies You Can Get from Boxes Problem
Description
LeetCode Problem 1298.
You have n boxes labeled from 0 to n - 1. You are given four arrays: status, candies, keys, and containedBoxes where:
status[i]is1if thei^thbox is open and0if thei^thbox is closed,candies[i]is the number of candies in thei^thbox,keys[i]is a list of the labels of the boxes you can open after opening thei^thbox.containedBoxes[i]is a list of the boxes you found inside thei^thbox.
You are given an integer array initialBoxes that contains the labels of the boxes you initially have. You can take all the candies in any open box and you can use the keys in it to open new boxes and you also can use the boxes you find in it.
Return the maximum number of candies you can get following the rules above.
Example 1:
Input: status = [1,0,1,0], candies = [7,5,4,100], keys = [[],[],[1],[]], containedBoxes = [[1,2],[3],[],[]], initialBoxes = [0]
Output: 16
Explanation: You will be initially given box 0. You will find 7 candies in it and boxes 1 and 2.
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.
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.
Total number of candies collected = 7 + 4 + 5 = 16 candy.
Example 2:
Input: 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]
Output: 6
Explanation: You have initially box 0. Opening it you can find boxes 1,2,3,4 and 5 and their keys.
The total number of candies will be 6.
Constraints:
n == status.length == candies.length == keys.length == containedBoxes.length1 <= n <= 1000status[i]is either0or1.1 <= candies[i] <= 10000 <= keys[i].length <= n0 <= keys[i][j] < n- All values of
keys[i]are unique. 0 <= containedBoxes[i].length <= n0 <= containedBoxes[i][j] < n- All values of
containedBoxes[i]are unique. - Each box is contained in one box at most.
0 <= initialBoxes.length <= n0 <= initialBoxes[i] < n
Solution
Approach: BFS Over Boxes We Can Open
A box can be opened once two things are true: we hold it, and it is unlocked (open from the start, or we found its key). These two conditions can become true in either order, so we keep three flags per box:
has[b]: we hold boxb;unlocked[b]:status[b] == 1or we found its key;opened[b]: we already opened it and took its candies.
A helper tryOpen(b) opens box b (pushes it onto a queue) as soon as it is held and unlocked and not yet opened. We call it for every initial box, and then process the queue: for a box taken from the queue, add its candies; for every key inside, mark that box unlocked and call tryOpen; for every box inside, mark it held and call tryOpen.
Checking at both moments is what makes this correct: a key that arrives before its box, and a box that arrives before its key, both end up opened when the second piece shows up. Each box enters the queue at most once thanks to opened, so its candies are counted once. Boxes that stay closed or are never found simply never enter the queue.
Complexity: O(n + total number of keys and contained boxes) time and O(n) space.
from collections import deque
class Solution:
def maxCandies(self, status: List[int], candies: List[int], keys: List[List[int]], containedBoxes: List[List[int]], initialBoxes: List[int]) -> int:
n = len(status)
has = [False] * n # we hold the box
unlocked = status[:] # the box is open or we hold its key
opened = [False] * n # we already took its candies
q = deque()
def try_open(b):
if has[b] and unlocked[b] and not opened[b]:
opened[b] = True
q.append(b)
for b in initialBoxes:
has[b] = True
try_open(b)
total = 0
while q:
box = q.popleft()
total += candies[box]
for k in keys[box]:
unlocked[k] = 1
try_open(k)
for b in containedBoxes[box]:
has[b] = True
try_open(b)
return total