|
| 1 | +<!--|This file generated by command(leetcode description); DO NOT EDIT. |--> |
| 2 | +<!--+----------------------------------------------------------------------+--> |
| 3 | +<!--|@author openset <[email protected]> |--> |
| 4 | +<!--|@link https://github.com/openset |--> |
| 5 | +<!--|@home https://github.com/openset/leetcode |--> |
| 6 | +<!--+----------------------------------------------------------------------+--> |
| 7 | + |
| 8 | +[< Previous](../maximum-number-of-occurrences-of-a-substring "Maximum Number of Occurrences of a Substring") |
| 9 | + |
| 10 | +Next > |
| 11 | + |
| 12 | +## [1298. Maximum Candies You Can Get from Boxes (Hard)](https://leetcode.com/problems/maximum-candies-you-can-get-from-boxes "你能从盒子里获得的最大糖果数") |
| 13 | + |
| 14 | +<p>Given <code>n</code> boxes, each box is given in the format <code>[status, candies, keys, containedBoxes]</code> where:</p> |
| 15 | + |
| 16 | +<ul> |
| 17 | + <li><code>status[i]</code>: an integer which is <strong>1</strong> if <code>box[i]</code> is open and <strong>0</strong> if <code>box[i]</code> is closed.</li> |
| 18 | + <li><code>candies[i]</code>: an integer representing the number of candies in <code>box[i]</code>.</li> |
| 19 | + <li><code>keys[i]</code>: an array contains the indices of the boxes you can open with the key in <code>box[i]</code>.</li> |
| 20 | + <li><code>containedBoxes[i]</code>: an array contains the indices of the boxes found in <code>box[i]</code>.</li> |
| 21 | +</ul> |
| 22 | + |
| 23 | +<p>You will start with some boxes given in <code>initialBoxes</code> 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.</p> |
| 24 | + |
| 25 | +<p>Return <em>the maximum number of candies</em> you can get following the rules above.</p> |
| 26 | + |
| 27 | +<p> </p> |
| 28 | +<p><strong>Example 1:</strong></p> |
| 29 | + |
| 30 | +<pre> |
| 31 | +<strong>Input:</strong> status = [1,0,1,0], candies = [7,5,4,100], keys = [[],[],[1],[]], containedBoxes = [[1,2],[3],[],[]], initialBoxes = [0] |
| 32 | +<strong>Output:</strong> 16 |
| 33 | +<strong>Explanation:</strong> 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. |
| 34 | +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. |
| 35 | +Total number of candies collected = 7 + 4 + 5 = 16 candy. |
| 36 | +</pre> |
| 37 | + |
| 38 | +<p><strong>Example 2:</strong></p> |
| 39 | + |
| 40 | +<pre> |
| 41 | +<strong>Input:</strong> 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] |
| 42 | +<strong>Output:</strong> 6 |
| 43 | +<strong>Explanation:</strong> 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. |
| 44 | +</pre> |
| 45 | + |
| 46 | +<p><strong>Example 3:</strong></p> |
| 47 | + |
| 48 | +<pre> |
| 49 | +<strong>Input:</strong> status = [1,1,1], candies = [100,1,100], keys = [[],[0,2],[]], containedBoxes = [[],[],[]], initialBoxes = [1] |
| 50 | +<strong>Output:</strong> 1 |
| 51 | +</pre> |
| 52 | + |
| 53 | +<p><strong>Example 4:</strong></p> |
| 54 | + |
| 55 | +<pre> |
| 56 | +<strong>Input:</strong> status = [1], candies = [100], keys = [[]], containedBoxes = [[]], initialBoxes = [] |
| 57 | +<strong>Output:</strong> 0 |
| 58 | +</pre> |
| 59 | + |
| 60 | +<p><strong>Example 5:</strong></p> |
| 61 | + |
| 62 | +<pre> |
| 63 | +<strong>Input:</strong> status = [1,1,1], candies = [2,3,2], keys = [[],[],[]], containedBoxes = [[],[],[]], initialBoxes = [2,1,0] |
| 64 | +<strong>Output:</strong> 7 |
| 65 | +</pre> |
| 66 | + |
| 67 | +<p> </p> |
| 68 | +<p><strong>Constraints:</strong></p> |
| 69 | + |
| 70 | +<ul> |
| 71 | + <li><code>1 <= status.length <= 1000</code></li> |
| 72 | + <li><code>status.length == candies.length == keys.length == containedBoxes.length == n</code></li> |
| 73 | + <li><code>status[i]</code> is <code>0</code> or <code>1</code>.</li> |
| 74 | + <li><code>1 <= candies[i] <= 1000</code></li> |
| 75 | + <li><code><font face="monospace">0 <= keys[i].length <= status.length</font></code></li> |
| 76 | + <li><code>0 <= keys[i][j] < status.length</code></li> |
| 77 | + <li>All values in <code>keys[i]</code> are unique.</li> |
| 78 | + <li><code><font face="monospace">0 <= </font>containedBoxes<font face="monospace">[i].length <= status.length</font></code></li> |
| 79 | + <li><code>0 <= containedBoxes[i][j] < status.length</code></li> |
| 80 | + <li>All values in <code>containedBoxes[i]</code> are unique.</li> |
| 81 | + <li>Each box is contained in one box at most.</li> |
| 82 | + <li><code>0 <= initialBoxes.length <= status.length</code></li> |
| 83 | + <li><code><font face="monospace">0 <= initialBoxes[i] < status.length</font></code></li> |
| 84 | +</ul> |
| 85 | + |
| 86 | +### Related Topics |
| 87 | + [[Breadth-first Search](../../tag/breadth-first-search/README.md)] |
| 88 | + |
| 89 | +### Hints |
| 90 | +<details> |
| 91 | +<summary>Hint 1</summary> |
| 92 | +Use Breadth First Search (BFS) to traverse all possible boxes you can open. Only push to the queue the boxes the you have with their keys. |
| 93 | +</details> |
0 commit comments