-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathsolution.js
More file actions
36 lines (33 loc) · 933 Bytes
/
Copy pathsolution.js
File metadata and controls
36 lines (33 loc) · 933 Bytes
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
/**
* JavaScript solution using Kahn's algorithm.
* @param {number} n
* @param {number[][]} prerequisites
* @returns {number[]}
*/
class Solution {
findOrder(n, prerequisites) {
// build adjacency list and indegree
const adj = Array.from({ length: n }, () => []);
const indeg = new Array(n).fill(0);
for (const pr of prerequisites) {
const x = pr[0],
y = pr[1]; // y -> x
adj[y].push(x);
indeg[x] += 1;
}
// queue implemented with head index for O(1) pops
const q = [];
let head = 0;
for (let i = 0; i < n; ++i) if (indeg[i] === 0) q.push(i);
const order = [];
while (head < q.length) {
const node = q[head++]; // pop
order.push(node);
for (const nei of adj[node]) {
indeg[nei] -= 1;
if (indeg[nei] === 0) q.push(nei);
}
}
return order.length === n ? order : [];
}
}