Link: https://leetcode.com/problems/find-eventual-safe-states/
Solution:
Topics: topological order
Intuition
Very nice topological order problem. This one took me a couple attempts before finding an efficient enough solution, although I had the right idea from the start.
The key idea is basically to do a cycle detection on each node. If there exists a cycle starting at that node, it is not a safe node. We can use a classic visiting/visited pattern here.
Implementation
Review 1
Fun little problem. Solved this one fast on first try…no idea why it took me a couple tries last time I solved this. Can also be solved with kahn’s algorithm.