-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathFindEventualSafeStates.java
More file actions
62 lines (50 loc) · 1.7 KB
/
FindEventualSafeStates.java
File metadata and controls
62 lines (50 loc) · 1.7 KB
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
package graph;
import java.util.ArrayList;
import java.util.Deque;
import java.util.LinkedList;
import java.util.List;
/**
* Description: https://leetcode.com/problems/find-eventual-safe-states
* Difficulty: Medium
* Time complexity: O(n)
* Space complexity: O(1)
*/
public class FindEventualSafeStates {
private static final int WHITE = 0;
private static final int GREY = 1;
private static final int BLACK = 2;
public List<Integer> eventualSafeNodes(int[][] graph) {
int[] visited = new int[graph.length];
List<Integer> safeNodes = new ArrayList<>();
for (int node = 0; node < graph.length; node++) {
if (!hasCycle(graph, visited, node)) {
safeNodes.add(node);
}
}
return safeNodes;
}
private boolean hasCycle(int[][] graph, int[] visited, int start) {
if (visited[start] != WHITE) {
// if we've already visited the node, check if we were able to color it BLACK
return visited[start] == GREY;
}
Deque<Integer> stack = new LinkedList<>();
stack.push(start);
while (!stack.isEmpty()) {
int current = stack.pop();
if (visited[current] == WHITE) {
visited[current] = GREY;
stack.push(current);
for (int neighbor : graph[current]) {
if (visited[neighbor] == GREY) return true;
if (visited[neighbor] == WHITE) {
stack.push(neighbor);
}
}
} else if (visited[current] == GREY) {
visited[current] = BLACK;
}
}
return false;
}
}