{"id":5984,"date":"2019-12-28T07:45:17","date_gmt":"2019-12-28T15:45:17","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=5984"},"modified":"2019-12-28T09:03:23","modified_gmt":"2019-12-28T17:03:23","slug":"leetcode-1298-maximum-candies-you-can-get-from-boxes","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/searching\/leetcode-1298-maximum-candies-you-can-get-from-boxes\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 1298. Maximum Candies You Can Get from Boxes"},"content":{"rendered":"\n<figure class=\"wp-block-embed-youtube wp-block-embed is-type-video is-provider-youtube wp-embed-aspect-4-3 wp-has-aspect-ratio\"><div class=\"wp-block-embed__wrapper\">\n<iframe loading=\"lazy\" title=\"\u82b1\u82b1\u9171 LeetCode 1298. Maximum Candies You Can Get from Boxes - \u5237\u9898\u627e\u5de5\u4f5c EP288\" width=\"500\" height=\"375\" src=\"https:\/\/www.youtube.com\/embed\/5_32fbbLx5U?feature=oembed\" frameborder=\"0\" allow=\"accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share\" referrerpolicy=\"strict-origin-when-cross-origin\" allowfullscreen><\/iframe>\n<\/div><\/figure>\n\n\n\n<p>Given&nbsp;<code>n<\/code>&nbsp;boxes, each box is given in the format&nbsp;<code>[status, candies, keys, containedBoxes]<\/code>&nbsp;where:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code>status[i]<\/code>: an integer which is&nbsp;<strong>1<\/strong>&nbsp;if&nbsp;<code>box[i]<\/code>&nbsp;is open and&nbsp;<strong>0<\/strong>&nbsp;if&nbsp;<code>box[i]<\/code>&nbsp;is closed.<\/li><li><code>candies[i]<\/code>:&nbsp;an integer representing the number of candies in&nbsp;<code>box[i]<\/code>.<\/li><li><code>keys[i]<\/code>: an array contains the indices of the boxes you can open with the key in&nbsp;<code>box[i]<\/code>.<\/li><li><code>containedBoxes[i]<\/code>: an array contains the indices of the boxes found in&nbsp;<code>box[i]<\/code>.<\/li><\/ul>\n\n\n\n<p>You will start with some boxes given in&nbsp;<code>initialBoxes<\/code>&nbsp;array. You can take all the candies in any open&nbsp;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>\n\n\n\n<p>Return&nbsp;<em>the maximum number of candies<\/em>&nbsp;you can get following the rules above.<\/p>\n\n\n\n<p><strong>Example 1:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> status = [1,0,1,0], candies = [7,5,4,100], keys = [[],[],[1],[]], containedBoxes = [[1,2],[3],[],[]], initialBoxes = [0]\n<strong>Output:<\/strong> 16\n<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.\nIn 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.\nTotal number of candies collected = 7 + 4 + 5 = 16 candy.\n<\/pre>\n\n\n\n<p><strong>Example 2:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><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]\n<strong>Output:<\/strong> 6\n<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.\n<\/pre>\n\n\n\n<p><strong>Example 3:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> status = [1,1,1], candies = [100,1,100], keys = [[],[0,2],[]], containedBoxes = [[],[],[]], initialBoxes = [1]\n<strong>Output:<\/strong> 1\n<\/pre>\n\n\n\n<p><strong>Example 4:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> status = [1], candies = [100], keys = [[]], containedBoxes = [[]], initialBoxes = []\n<strong>Output:<\/strong> 0\n<\/pre>\n\n\n\n<p><strong>Example 5:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> status = [1,1,1], candies = [2,3,2], keys = [[],[],[]], containedBoxes = [[],[],[]], initialBoxes = [2,1,0]\n<strong>Output:<\/strong> 7\n<\/pre>\n\n\n\n<p><strong>Constraints:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code>1 &lt;= status.length &lt;= 1000<\/code><\/li><li><code>status.length == candies.length == keys.length == containedBoxes.length == n<\/code><\/li><li><code>status[i]<\/code>&nbsp;is&nbsp;<code>0<\/code>&nbsp;or&nbsp;<code>1<\/code>.<\/li><li><code>1 &lt;= candies[i] &lt;= 1000<\/code><\/li><li><code>0 &lt;= keys[i].length &lt;= status.length<\/code><\/li><li><code>0 &lt;= keys[i][j] &lt; status.length<\/code><\/li><li>All values in&nbsp;<code>keys[i]<\/code>&nbsp;are unique.<\/li><li><code>0 &lt;=&nbsp;containedBoxes[i].length &lt;= status.length<\/code><\/li><li><code>0 &lt;= containedBoxes[i][j] &lt; status.length<\/code><\/li><li>All values in&nbsp;<code>containedBoxes[i]<\/code>&nbsp;are unique.<\/li><li>Each box is contained in one box at most.<\/li><li><code>0 &lt;= initialBoxes.length&nbsp;&lt;= status.length<\/code><\/li><li><code>0 &lt;= initialBoxes[i] &lt; status.length<\/code><\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution: BFS<\/strong><\/h2>\n\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" decoding=\"async\" width=\"960\" height=\"540\" src=\"https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2019\/12\/1298-ep288.png\" alt=\"\" class=\"wp-image-5992\" srcset=\"https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2019\/12\/1298-ep288.png 960w, https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2019\/12\/1298-ep288-300x169.png 300w, https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2019\/12\/1298-ep288-768x432.png 768w\" sizes=\"auto, (max-width: 960px) 100vw, 960px\" \/><\/figure>\n\n\n\n<p>Only push boxes that we can open into the queue.<\/p>\n\n\n\n<p>When can we open the box?<br>1. When we find it and have the key in hand.<br>2. When we find the key and have the box in hand.<\/p>\n\n\n\n<p>Time complexity: O(B + K)<br>Space complexity: O(B)<\/p>\n\n\n\n<div class=\"responsive-tabs\">\n<h2 class=\"tabtitle\">C++<\/h2>\n<div class=\"tabcontent\">\n\n<pre lang=\"c++\">\n\/\/ Author: Huahua\nclass Solution {\npublic:\n    int maxCandies(vector<int>& status, vector<int>& candies, vector<vector<int>>& keys, vector<vector<int>>& containedBoxes, vector<int>& initialBoxes) {      \n      vector<int> found(status.size());\n      vector<int> hasKeys(status);\n      queue<int> q;\n      \n      for (int b : initialBoxes) {\n        found[b] = 1;\n        if (hasKeys[b]) q.push(b);\n      }\n  \n      int ans = 0;\n      while (!q.empty()) {\n        int b = q.front();\n        q.pop();\n        ans += candies[b];\n        for (int t : containedBoxes[b]) {\n          found[t] = 1;\n          if (hasKeys[t]) q.push(t);\n        }\n        for (int t : keys[b]) {\n          if (!hasKeys[t] && found[t]) q.push(t);\n          hasKeys[t] = 1;\n        }\n      }\n\n      return ans;\n    }\n};\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>Given&nbsp;n&nbsp;boxes, each box is given in the format&nbsp;[status, candies, keys, containedBoxes]&nbsp;where: status[i]: an integer which is&nbsp;1&nbsp;if&nbsp;box[i]&nbsp;is open and&nbsp;0&nbsp;if&nbsp;box[i]&nbsp;is closed. candies[i]:&nbsp;an integer representing the number of&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[44],"tags":[34,77,217],"class_list":["post-5984","post","type-post","status-publish","format-standard","hentry","category-searching","tag-bfs","tag-graph","tag-hard","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/5984","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/comments?post=5984"}],"version-history":[{"count":4,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/5984\/revisions"}],"predecessor-version":[{"id":5993,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/5984\/revisions\/5993"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=5984"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=5984"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=5984"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}