{"id":6732,"date":"2020-05-10T00:55:33","date_gmt":"2020-05-10T07:55:33","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=6732"},"modified":"2020-05-10T11:41:16","modified_gmt":"2020-05-10T18:41:16","slug":"leetcode-1443-minimum-time-to-collect-all-apples-in-a-tree","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/uncategorized\/leetcode-1443-minimum-time-to-collect-all-apples-in-a-tree\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 1443. Minimum Time to Collect All Apples in a Tree"},"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 1443. Minimum Time to Collect All Apples in a Tree - \u5237\u9898\u627e\u5de5\u4f5c EP324\" width=\"500\" height=\"375\" src=\"https:\/\/www.youtube.com\/embed\/VhDs6LRNWeA?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 an undirected tree consisting of&nbsp;<code>n<\/code>&nbsp;vertices numbered from 0 to&nbsp;<code>n-1<\/code>, which has some apples in their&nbsp;vertices. You spend 1 second to walk over one&nbsp;edge of the tree.&nbsp;<em>Return the minimum time in seconds&nbsp;you have to spend&nbsp;in order to collect all apples in the tree starting at&nbsp;<strong>vertex 0<\/strong>&nbsp;and coming back to this vertex.<\/em><\/p>\n\n\n\n<p>The edges of the undirected tree are given in the array&nbsp;<code>edges<\/code>, where&nbsp;<code>edges[i] = [from<sub>i<\/sub>, to<sub>i<\/sub>]<\/code>&nbsp;means that exists an edge connecting the vertices&nbsp;<code>from<sub>i<\/sub><\/code>&nbsp;and&nbsp;<code>to<sub>i<\/sub><\/code>. Additionally, there is&nbsp;a boolean array&nbsp;<code>hasApple<\/code>, where&nbsp;<code>hasApple[i] = true<\/code>&nbsp;means that&nbsp;vertex&nbsp;<code>i<\/code>&nbsp;has an apple, otherwise, it does not have any apple.<\/p>\n\n\n\n<p><strong>Example 1:<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/assets.leetcode.com\/uploads\/2020\/04\/23\/min_time_collect_apple_1.png\" alt=\"\"\/><\/figure>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> n = 7, edges = [[0,1],[0,2],[1,4],[1,5],[2,3],[2,6]], hasApple = [false,false,true,false,true,true,false]\n<strong>Output:<\/strong> 8 \n<strong>Explanation:<\/strong> The figure above represents the given tree where red vertices have an apple. One optimal path to collect all apples is shown by the green arrows.  \n<\/pre>\n\n\n\n<p><strong>Example 2:<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/assets.leetcode.com\/uploads\/2020\/04\/23\/min_time_collect_apple_2.png\" alt=\"\"\/><\/figure>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> n = 7, edges = [[0,1],[0,2],[1,4],[1,5],[2,3],[2,6]], hasApple = [false,false,true,false,false,true,false]\n<strong>Output:<\/strong> 6\n<strong>Explanation:<\/strong> The figure above represents the given tree where red vertices have an apple. One optimal path to collect all apples is shown by the green arrows.  \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> n = 7, edges = [[0,1],[0,2],[1,4],[1,5],[2,3],[2,6]], hasApple = [false,false,false,false,false,false,false]\n<strong>Output:<\/strong> 0\n<\/pre>\n\n\n\n<p><strong>Constraints:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code>1 &lt;= n &lt;= 10^5<\/code><\/li><li><code>edges.length == n-1<\/code><\/li><li><code>edges[i].length == 2<\/code><\/li><li><code>0 &lt;= from<sub>i<\/sub>, to<sub>i<\/sub>&nbsp;&lt;= n-1<\/code><\/li><li><code>from<sub>i<\/sub>&nbsp;&lt; to<sub>i<\/sub><\/code><\/li><li><code>hasApple.length == n<\/code><\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution: DFS<\/strong><\/h2>\n\n\n\n<p>Build the graph (tree) and DFS.<\/p>\n\n\n\n<p>Time complexity: O(n)<br>Space complexity: O(n)<\/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 minTime(int n, vector<vector<int>>& edges, vector<bool>& hasApple) {\n    vector<vector<int>> g(n);\n    for (const auto& e : edges)\n      g[e[0]].push_back(e[1]);    \n        \n    function<int(int)> dfs = [&] (int cur) {      \n      int total = 0;\n      for (int nxt : g[cur]) {        \n        int cost = dfs(nxt);\n        if (cost > 0 || hasApple[nxt]) total += 2 + cost;\n      }      \n      return total;\n    };\n    \n    return dfs(0);\n  }\n};\n<\/pre>\n<\/div><\/div>\n\n\n\n<h2 class=\"wp-block-heading\">if edge is not ordered<\/h2>\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 minTime(int n, vector<vector<int>>& edges, \n              vector<bool>& hasApple) {\n    vector<vector<int>> g(n);\n    for (const auto& e : edges) {\n      g[e[0]].push_back(e[1]);\n      g[e[1]].push_back(e[0]);\n    }\n    vector<int> seen(n);    \n    function<int(int)> dfs = [&] (int cur) {\n      seen[cur] = 1;\n      int total = 0;      \n      for (int child : g[cur]) {\n        if (seen[child]) continue;        \n        int cost = dfs(child);\n        if (cost > 0 || hasApple[child])          \n          total += 2 + cost;        \n      }      \n      return total;\n    };    \n    return dfs(0);    \n  }\n};\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>Given an undirected tree consisting of&nbsp;n&nbsp;vertices numbered from 0 to&nbsp;n-1, which has some apples in their&nbsp;vertices. You spend 1 second to walk over one&nbsp;edge 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":[1],"tags":[],"class_list":["post-6732","post","type-post","status-publish","format-standard","hentry","category-uncategorized","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/6732","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=6732"}],"version-history":[{"count":3,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/6732\/revisions"}],"predecessor-version":[{"id":6735,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/6732\/revisions\/6735"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=6732"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=6732"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=6732"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}