MCPcopy Create free account
hub / github.com/Tiwarishashwat/InterviewCodes / Solution

Class Solution

FindEventualSafeStates.java:1–29  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

1class Solution {
2 public List<Integer> eventualSafeNodes(int[][] graph) {
3 // V+E
4 // V
5 HashMap<Integer,Boolean> map = new HashMap<>();
6 int n = graph.length;
7 List<Integer> res = new ArrayList<>();
8 for(int i=0;i<n;i++){
9 if(dfs(i,graph,map)){
10 res.add(i);
11 }
12 }
13 return res;
14 }
15 public boolean dfs(int node, int[][] graph, HashMap<Integer,Boolean> map){
16 if(map.containsKey(node)){
17 return map.get(node);
18 }
19
20 map.put(node,false);
21 for(int neighbour : graph[node]){
22 if(!dfs(neighbour, graph, map)){
23 return false;
24 }
25 }
26 map.put(node,true);
27 return true;
28 }
29}

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected