We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
1 parent bc735c9 commit b142dbeCopy full SHA for b142dbe
802-find-eventual-safe-states.js
@@ -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
34
/**
35
* @param {number[][]} graph
36
* @return {number[]}
0 commit comments