Skip to content

Commit b142dbe

Browse files
authored
Update 802-find-eventual-safe-states.js
1 parent bc735c9 commit b142dbe

File tree

1 file changed

+33
-0
lines changed

1 file changed

+33
-0
lines changed

802-find-eventual-safe-states.js

+33
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,36 @@
1+
/**
2+
* @param {number[][]} graph
3+
* @return {number[]}
4+
*/
5+
const eventualSafeNodes = function (graph) {
6+
const ing = {},
7+
n = graph.length
8+
const outDegree = Array(n).fill(0)
9+
let q = []
10+
for (let i = 0; i < n; i++) {
11+
outDegree[i] = graph[i].length
12+
if (outDegree[i] === 0) {
13+
q.push(i)
14+
}
15+
for (const e of graph[i]) {
16+
if (ing[e] == null) ing[e] = []
17+
ing[e].push(i)
18+
}
19+
}
20+
21+
for (const term of q) {
22+
for (const come of ing[term] || []) {
23+
outDegree[come]--
24+
if (outDegree[come] === 0) q.push(come)
25+
}
26+
}
27+
q.sort((a, b) => a - b)
28+
return q
29+
}
30+
31+
// another
32+
33+
134
/**
235
* @param {number[][]} graph
336
* @return {number[]}

0 commit comments

Comments
 (0)