Description
You have n boxes labeled from 0 to n - 1. You are given four arrays: status, candies, keys, and containedBoxes where:
status[i]is1if theithbox is open and0if theithbox is closed,candies[i]is the number of candies in theithbox,keys[i]is a list of the labels of the boxes you can open after opening theithbox.containedBoxes[i]is a list of the boxes you found inside theithbox.
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
Solutions
Solution 1: BFS + Hash Set
The problem gives a set of boxes, each of which may have a state (open/closed), candies, keys, and other boxes inside. Our goal is to use the initially given boxes to open as many more boxes as possible and collect the candies inside. We can unlock new boxes by obtaining keys, and get more resources through boxes nested inside other boxes.
We use BFS to simulate the entire exploration process.
We use a queue q to represent the currently accessible and already opened boxes; two sets, has and took, are used to record all boxes we own and boxes we have already processed, to avoid duplicates.
Initially, add all initialBoxes to has. If an initial box is open, immediately add it to the queue q and accumulate its candies.
Then perform BFS, taking boxes out of q one by one:
- Obtain the keys in the box keys[box] and add any boxes that can be unlocked to the queue;
- Collect other boxes contained in the box containedBoxes[box]. If a contained box is open and has not been processed, process it immediately;
Each box is processed at most once, and candies are accumulated once. Finally, return the total number of candies ans.
The time complexity is O(n), and the space complexity is O(n), where n is the total number of boxes.
class Solution: def maxCandies( self, status: List[int], candies: List[int], keys: List[List[int]], containedBoxes: List[List[int]], initialBoxes: List[int], ) -> int: q = deque() has, took = set(initialBoxes), set() ans = 0 for box in initialBoxes: if status[box]: q.append(box) took.add(box) ans += candies[box] while q: box = q.popleft() for k in keys[box]: if not status[k]: status[k] = 1 if k in has and k not in took: q.append(k) took.add(k) ans += candies[k] for b in containedBoxes[box]: has.add(b) if status[b] and b not in took: q.append(b) took.add(b) ans += candies[b] return ans(code-box)
