Given n
boxes, each box is given in the format [status, candies, keys, containedBoxes]
where:
status[i]
: an integer which is 1 ifbox[i]
is open and 0 ifbox[i]
is closed.candies[i]
: an integer representing the number of candies inbox[i]
.keys[i]
: an array contains the indices of the boxes you can open with the key inbox[i]
.containedBoxes[i]
: an array contains the indices of the boxes found inbox[i]
.
You will start with some boxes given in initialBoxes
array. 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 don't 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.
Example 3:
Input: status = [1,1,1], candies = [100,1,100], keys = [[],[0,2],[]], containedBoxes = [[],[],[]], initialBoxes = [1] Output: 1
Example 4:
Input: status = [1], candies = [100], keys = [[]], containedBoxes = [[]], initialBoxes = [] Output: 0
Example 5:
Input: status = [1,1,1], candies = [2,3,2], keys = [[],[],[]], containedBoxes = [[],[],[]], initialBoxes = [2,1,0] Output: 7
Constraints:
1 <= status.length <= 1000
status.length == candies.length == keys.length == containedBoxes.length == n
status[i]
is0
or1
.1 <= candies[i] <= 1000
0 <= keys[i].length <= status.length
0 <= keys[i][j] < status.length
- All values in
keys[i]
are unique. 0 <= containedBoxes[i].length <= status.length
0 <= containedBoxes[i][j] < status.length
- All values in
containedBoxes[i]
are unique. - Each box is contained in one box at most.
0 <= initialBoxes.length <= status.length
0 <= initialBoxes[i] < status.length
Solution: BFS
Only push boxes that we can open into the queue.
When can we open the box?
1. When we find it and have the key in hand.
2. When we find the key and have the box in hand.
Time complexity: O(B + K)
Space complexity: O(B)
C++
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
// Author: Huahua class Solution { public: int maxCandies(vector<int>& status, vector<int>& candies, vector<vector<int>>& keys, vector<vector<int>>& containedBoxes, vector<int>& initialBoxes) { vector<int> found(status.size()); vector<int> hasKeys(status); queue<int> q; for (int b : initialBoxes) { found[b] = 1; if (hasKeys[b]) q.push(b); } int ans = 0; while (!q.empty()) { int b = q.front(); q.pop(); ans += candies[b]; for (int t : containedBoxes[b]) { found[t] = 1; if (hasKeys[t]) q.push(t); } for (int t : keys[b]) { if (!hasKeys[t] && found[t]) q.push(t); hasKeys[t] = 1; } } return ans; } }; |
请尊重作者的劳动成果,转载请注明出处!花花保留对文章/视频的所有权利。
如果您喜欢这篇文章/视频,欢迎您捐赠花花。
If you like my articles / videos, donations are welcome.
Be First to Comment