Find Eventual Safe States
Medium · Graphs · about 20 minutes
Asked at Uber, Microsoft, Amazon, Google.
A directed graph has n nodes 0..n-1; graph[i] lists the nodes that i has edges to. A node is safe if every path starting from it leads to a terminal node (one with no outgoing edges), i.e. it can't reach a cycle. Return all safe nodes in ascending order.