Skip to content

Commit be711b9

Browse files
authored
Create 1376-time-needed-to-inform-all-employees.js
1 parent ade6f75 commit be711b9

File tree

1 file changed

+35
-0
lines changed

1 file changed

+35
-0
lines changed
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/**
2+
* @param {number} n
3+
* @param {number} headID
4+
* @param {number[]} manager
5+
* @param {number[]} informTime
6+
* @return {number}
7+
*/
8+
const numOfMinutes = function(n, headID, manager, informTime) {
9+
const hash = {}
10+
const len = manager.length
11+
for(let i = 0; i < len; i++) {
12+
const m = manager[i]
13+
if(hash[m] == null) hash[m] = new Set()
14+
hash[m].add(i)
15+
}
16+
let res = 0
17+
let q = [[headID, 0]]
18+
while(q.length) {
19+
const tmp = []
20+
let t = 0
21+
const size = q.length
22+
for(let i = 0; i < size; i++) {
23+
const [cur, time] = q[i]
24+
if(hash[cur]) {
25+
for(const e of hash[cur]) {
26+
res = Math.max(res, time + informTime[cur])
27+
tmp.push([e, time + informTime[cur]])
28+
}
29+
}
30+
}
31+
q = tmp
32+
res += t
33+
}
34+
return res
35+
};

0 commit comments

Comments
 (0)