LeetCode-1298. Maximum Candies You Can Get from Boxes

网友投稿 729 2022-10-03

LeetCode-1298. Maximum Candies You Can Get from Boxes

LeetCode-1298. Maximum Candies You Can Get from Boxes

Given ​​n​​​ boxes, each box is given in the format ​​[status, candies, keys, containedBoxes]​​ where:

​​status[i]​​: an integer which is1if​​box[i]​​ is open and0if​​box[i]​​ is closed.​​candies[i]​​​: an integer representing the number of candies in​​box[i]​​.​​keys[i]​​​: an array contains the indices of the boxes you can open with the key in​​box[i]​​.​​containedBoxes[i]​​​: an array contains the indices of the boxes found in​​box[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: 16Explanation: 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: 6Explanation: 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]​​​ is​​0​​​ or​​1​​.​​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​​

​​题解:​​

​​bfs,使用两个数组保存已有箱子和钥匙,每次开完箱子都进行查找是否有钥匙跟箱子匹配。​​

class Solution {public: int maxCandies(vector& status, vector& candies, vector>& keys, vector>& containedBoxes, vector& initialBoxes) { int n = status.size(); queue q; vector box(n, false); vector key(n, false); int res = 0; for (int i = 0; i < initialBoxes.size(); i++) { if (status[initialBoxes[i]] == 1) { q.push(initialBoxes[i]); } if (status[initialBoxes[i]] == 0) { box[initialBoxes[i]] = true; } } while (q.empty() == false) { int t = q.front(); q.pop(); res += candies[t]; for (int i = 0; i < keys[t].size(); i++) { key[keys[t][i]] = true; } for (int i = 0; i < containedBoxes[t].size(); i++) { if (status[containedBoxes[t][i]] == 1) { q.push(containedBoxes[t][i]); } if (status[containedBoxes[t][i]] == 0) { box[containedBoxes[t][i]] = true; } } for (int i = 0; i < n; i++) { if (key[i] == true && box[i] == true) { q.push(i); key[i] = false; box[i] = false; } } } return res; }};

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:SpringBoot整合RabbitMQ处理死信队列和延迟队列
下一篇:LeetCode-1221. Split a String in Balanced Strings
相关文章

 发表评论

暂时没有评论,来抢沙发吧~