Skip to content

Commit ec07531

Browse files
authored
Create 2050-parallel-courses-iii.js
1 parent 8548592 commit ec07531

File tree

1 file changed

+36
-0
lines changed

1 file changed

+36
-0
lines changed

2050-parallel-courses-iii.js

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/**
2+
* @param {number} n
3+
* @param {number[][]} relations
4+
* @param {number[]} time
5+
* @return {number}
6+
*/
7+
const minimumTime = function(n, relations, time) {
8+
const graph = {}, dist = Array(n).fill(0), inDegree = Array(n).fill(0)
9+
for(let [from, to] of relations) {
10+
from--, to--
11+
if (graph[from] == null) graph[from] = []
12+
graph[from].push(to)
13+
inDegree[to]++
14+
}
15+
const q = []
16+
for(let i = 0; i < n; i++) {
17+
if(inDegree[i] === 0) {
18+
q.push(i)
19+
dist[i] = time[i]
20+
}
21+
}
22+
23+
while(q.length) {
24+
const u = q.shift()
25+
for(const v of (graph[u] || [])) {
26+
dist[v] = Math.max(dist[v], dist[u] + time[v])
27+
if(--inDegree[v] === 0) q.push(v)
28+
}
29+
}
30+
31+
let res = 0
32+
for(let e of dist) {
33+
if(e > res) res = e
34+
}
35+
return res
36+
};

0 commit comments

Comments
 (0)