| 1 | class 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 | } |
nothing calls this directly
no outgoing calls
no test coverage detected