{"id":6419,"date":"2020-03-08T12:09:17","date_gmt":"2020-03-08T19:09:17","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=6419"},"modified":"2020-03-08T12:22:23","modified_gmt":"2020-03-08T19:22:23","slug":"leetcode-1376-time-needed-to-inform-all-employees","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/searching\/leetcode-1376-time-needed-to-inform-all-employees\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 1376. Time Needed to Inform All Employees"},"content":{"rendered":"\n<p>A company has&nbsp;<code>n<\/code>&nbsp;employees with a unique ID for each employee from&nbsp;<code>0<\/code>&nbsp;to&nbsp;<code>n - 1<\/code>. The head of the company has is the one with&nbsp;<code>headID<\/code>.<\/p>\n\n\n\n<p>Each employee has one&nbsp;direct manager given in the&nbsp;<code>manager<\/code>&nbsp;array where&nbsp;<code>manager[i]<\/code>&nbsp;is the direct manager of the&nbsp;<code>i-th<\/code>&nbsp;employee,&nbsp;<code>manager[headID] = -1<\/code>. Also it&#8217;s guaranteed that the subordination relationships have a tree structure.<\/p>\n\n\n\n<p>The head of the company wants to inform all the employees of the company of an urgent piece of news. He will inform his direct subordinates and they will inform their subordinates and so on until all employees know about the urgent news.<\/p>\n\n\n\n<p>The&nbsp;<code>i-th<\/code>&nbsp;employee needs&nbsp;<code>informTime[i]<\/code>&nbsp;minutes to inform all of his direct subordinates (i.e After informTime[i] minutes, all his direct subordinates can start spreading the news).<\/p>\n\n\n\n<p>Return&nbsp;<em>the number of minutes<\/em>&nbsp;needed to inform all the employees about the urgent news.<\/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> n = 1, headID = 0, manager = [-1], informTime = [0]\n<strong>Output:<\/strong> 0\n<strong>Explanation:<\/strong> The head of the company is the only employee in the company.\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\/02\/27\/graph.png\" alt=\"\"\/><\/figure>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> n = 6, headID = 2, manager = [2,2,-1,2,2,2], informTime = [0,0,1,0,0,0]\n<strong>Output:<\/strong> 1\n<strong>Explanation:<\/strong> The head of the company with id = 2 is the direct manager of all the employees in the company and needs 1 minute to inform them all.\nThe tree structure of the employees in the company is shown.\n<\/pre>\n\n\n\n<p><strong>Example 3:<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/assets.leetcode.com\/uploads\/2020\/02\/28\/1730_example_3_5.PNG\" alt=\"\"\/><\/figure>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> n = 7, headID = 6, manager = [1,2,3,4,5,6,-1], informTime = [0,6,5,4,3,2,1]\n<strong>Output:<\/strong> 21\n<strong>Explanation:<\/strong> The head has id = 6. He will inform employee with id = 5 in 1 minute.\nThe employee with id = 5 will inform the employee with id = 4 in 2 minutes.\nThe employee with id = 4 will inform the employee with id = 3 in 3 minutes.\nThe employee with id = 3 will inform the employee with id = 2 in 4 minutes.\nThe employee with id = 2 will inform the employee with id = 1 in 5 minutes.\nThe employee with id = 1 will inform the employee with id = 0 in 6 minutes.\nNeeded time = 1 + 2 + 3 + 4 + 5 + 6 = 21.\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> n = 15, headID = 0, manager = [-1,0,0,1,1,2,2,3,3,4,4,5,5,6,6], informTime = [1,1,1,1,1,1,1,0,0,0,0,0,0,0,0]\n<strong>Output:<\/strong> 3\n<strong>Explanation:<\/strong> The first minute the head will inform employees 1 and 2.\nThe second minute they will inform employees 3, 4, 5 and 6.\nThe third minute they will inform the rest of employees.\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> n = 4, headID = 2, manager = [3,3,-1,2], informTime = [0,0,162,914]\n<strong>Output:<\/strong> 1076\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>0 &lt;= headID &lt; n<\/code><\/li><li><code>manager.length == n<\/code><\/li><li><code>0 &lt;= manager[i] &lt; n<\/code><\/li><li><code>manager[headID] == -1<\/code><\/li><li><code>informTime.length&nbsp;== n<\/code><\/li><li><code>0 &lt;= informTime[i] &lt;= 1000<\/code><\/li><li><code>informTime[i] == 0<\/code>&nbsp;if employee&nbsp;<code>i<\/code>&nbsp;has&nbsp;no subordinates.<\/li><li>It is&nbsp;<strong>guaranteed<\/strong>&nbsp;that all the employees can be informed.<\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution 1: Build the graph + DFS<\/strong><\/h2>\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 numOfMinutes(int n, int headID, vector<int>& manager, vector<int>& informTime) {\n    vector<vector<int>> es(n);\n    for (int i = 0; i < n; ++i)\n      if (i != headID) es[manager[i]].push_back(i);      \n    function<int(int)> dfs = [&](int cur) {\n      int t = 0;\n      for (int e : es[cur])\n        t = max(t, dfs(e));\n      return t + informTime[cur];\n    };\n    return dfs(headID);\n  }\n};\n<\/pre>\n<\/div><\/div>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution 2: Recursion with memoization<\/strong><\/h2>\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 numOfMinutes(int n, int headID, vector<int>& manager, vector<int>& informTime) {\n    vector<int> cache(n, -1);    \n    function<int(int)> dfs = [&](int cur) {\n      if (cur == -1) return 0;\n      if (cache[cur] == -1) \n        cache[cur] = dfs(manager[cur]) + informTime[cur];\n      return cache[cur];\n    };\n    int ans = 0;\n    for (int i = 0; i < n; ++i)\n      ans = max(ans, dfs(i));\n    return ans;\n  }\n};\n<\/pre>\n<\/div><\/div>\n\n\n\n<div class=\"responsive-tabs\">\n<h2 class=\"tabtitle\">Python3<\/h2>\n<div class=\"tabcontent\">\n\n<pre lang=\"python\">\n# Author: Huahua\nclass Solution:\n  def numOfMinutes(self, n: int, headID: int, manager: List[int], informTime: List[int]) -> int:\n    cache = [-1] * n\n    def dfs(cur):\n      if cur == -1: return 0\n      if cache[cur] == -1:\n        cache[cur] = dfs(manager[cur]) + informTime[cur]\n      return cache[cur]\n    return max([dfs(i) for i in range(n)])\n<\/pre>\n<\/div><\/div>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>A company has&nbsp;n&nbsp;employees with a unique ID for each employee from&nbsp;0&nbsp;to&nbsp;n &#8211; 1. The head of the company has is the one with&nbsp;headID. Each employee&#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":[33,177,17],"class_list":["post-6419","post","type-post","status-publish","format-standard","hentry","category-searching","tag-dfs","tag-medium","tag-recursion","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/6419","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=6419"}],"version-history":[{"count":3,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/6419\/revisions"}],"predecessor-version":[{"id":6422,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/6419\/revisions\/6422"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=6419"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=6419"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=6419"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}