【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 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: 1Example 4:
Input: status = [1], candies = [100], keys = [[]], containedBoxes = [[]], initialBoxes = [] Output: 0Example 5:
Input: status = [1,1,1], candies = [2,3,2], keys = [[],[],[]], containedBoxes = [[],[],[]], initialBoxes = [2,1,0] Output: 7Constraints:
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
解题思路:我的方法是维护两个列表,一个是待开启的box列表,一个是当前拥有的key的列表。每次遍历这两个列表,如果遇到箱子能开启,就开启,并且把新开启箱子得到的key和box再放入对应的列表中;如果遇到无法开启列表中任何一个箱子的情况,则表示已经没有箱子可开启,退出程序。
代码如下:
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 """ res = 0 to_be_opened = initialBoxes my_keys = [] flag = True while flag: flag = False for inx in range(len(to_be_opened) - 1,-1,-1): box = to_be_opened[inx] if status[box] == 1: res += candies[box] del to_be_opened[inx] to_be_opened += containedBoxes[box] my_keys += keys[box] flag = True continue for key_inx in range(len(my_keys)-1,-1,-1): if my_keys[key_inx] == box: res += candies[box] del to_be_opened[inx] del my_keys[key_inx] to_be_opened += containedBoxes[box] my_keys += keys[box] flag = True break return res
标签:box,status,candies,Get,Candies,keys,1298,key,containedBoxes 来源: https://www.cnblogs.com/seyjs/p/12150545.html