-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathsolution.js
More file actions
27 lines (27 loc) · 761 Bytes
/
Copy pathsolution.js
File metadata and controls
27 lines (27 loc) · 761 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
class Solution {
safeNodes(V, edges) {
const revGraph = Array.from({ length: V }, () => []);
const outdeg = new Array(V).fill(0);
// Build reverse graph and out-degree
for (const e of edges) {
const u = e[0],
v = e[1];
if (u < 0 || u >= V || v < 0 || v >= V) continue;
revGraph[v].push(u);
outdeg[u] += 1;
}
const q = [];
for (let i = 0; i < V; ++i) if (outdeg[i] === 0) q.push(i);
const safe = [];
while (q.length) {
const node = q.shift();
safe.push(node);
for (const parent of revGraph[node]) {
outdeg[parent] -= 1;
if (outdeg[parent] === 0) q.push(parent);
}
}
safe.sort((a, b) => a - b);
return safe;
}
}