forked from AnasImloul/Leetcode-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFind Eventual Safe States.java
40 lines (34 loc) · 1.08 KB
/
Find Eventual Safe States.java
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
// Runtime: 7 ms (Top 80.80%) | Memory: 65.9 MB (Top 65.16%)
class Solution {
public List<Integer> eventualSafeNodes(int[][] graph) {
int n=graph.length;
List<Integer> ans=new ArrayList<>();
boolean visited[]=new boolean[n];
boolean dfsVisited[]=new boolean[n];
boolean nodeCycles[]=new boolean[n];
for(int i=0;i<n;i++){
if(visited[i]==false){
isCycle(i,graph,dfsVisited,visited,nodeCycles);
}
}
for(int i=0;i<nodeCycles.length;i++){
if(nodeCycles[i]==false)
ans.add(i);
}
return ans;
}
public boolean isCycle(int node,int graph[][],boolean dfsVisited[],boolean visited[],boolean [] nodeCycles) {
visited[node]=true;
dfsVisited[node]=true;
for(int adjNode:graph[node]){
if(visited[adjNode]==false){
if(isCycle(adjNode,graph,dfsVisited,visited,nodeCycles))
return nodeCycles[node]=true;
}else if(visited[adjNode]==true && dfsVisited[adjNode]==true){
return nodeCycles[node]=true;
}
}
dfsVisited[node]=false;
return false;
}
}