Skip to content

Commit bee98e3

Browse files
authored
Update 2050-parallel-courses-iii.js
1 parent 1d6d10a commit bee98e3

File tree

1 file changed

+39
-0
lines changed

1 file changed

+39
-0
lines changed

2050-parallel-courses-iii.js

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,42 @@
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+
10+
for(let [pre, next] of relations) {
11+
pre--, next--
12+
if(graph[pre] == null) graph[pre] = []
13+
graph[pre].push(next)
14+
inDegree[next]++
15+
}
16+
17+
const q = []
18+
for(let i = 0; i < n; i++) {
19+
if(inDegree[i] === 0) {
20+
q.push(i)
21+
dist[i] = time[i]
22+
}
23+
}
24+
25+
let res = 0
26+
while(q.length) {
27+
const cur = q.shift()
28+
for(const next of (graph[cur] || [])) {
29+
dist[next] = Math.max(dist[next], dist[cur] + time[next])
30+
inDegree[next]--
31+
if(inDegree[next] === 0) q.push(next)
32+
}
33+
}
34+
35+
return Math.max(...dist)
36+
}
37+
38+
// another
39+
140
/**
241
* @param {number} n
342
* @param {number[][]} relations

0 commit comments

Comments
 (0)