forked from neetcode-gh/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1376-time-needed-to-inform-all-employees.js
More file actions
44 lines (36 loc) · 1.01 KB
/
1376-time-needed-to-inform-all-employees.js
File metadata and controls
44 lines (36 loc) · 1.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
/**
* DFS | Tree
* Time O(n) | Space O(n)
* https://leetcode.com/problems/time-needed-to-inform-all-employees/
* @param {number} n
* @param {number} headID
* @param {number[]} manager
* @param {number[]} informTime
* @return {number}
*/
var numOfMinutes = function (n, headID, manager, informTime) {
const tree = {};
for (let i = 0; i < manager.length; i++) {
if (manager[i] === -1) continue;
const senior = manager[i];
const junior = i;
if (!tree[senior]) {
tree[senior] = [];
}
tree[senior].push(junior);
}
let time = 0;
const dfs = (node, totalTime) => {
if (tree[node] === undefined) {
time = Math.max(time, totalTime);
return;
}
const subordinates = tree[node];
for (let i = 0; i < subordinates.length; i++) {
const subordinate = subordinates[i];
dfs(subordinate, totalTime + informTime[node]);
}
};
dfs(headID, 0);
return time;
};