Safe nodes
A directed graph has n nodes (labeled 0..n-1); graph[i] is the list of nodes i points to. A node is 'safe' if every possible path starting from it eventually reaches a terminal node (a node with no outgoing edges) — equivalently, no path from it can ever enter a cycle. Return all safe nodes in ascending order.
Implement
eventual_safe_nodes(graph: list[list[int]]) → list[int]Examples
in
[[[1,2],[2,3],[5],[0],[5],[],[]]]out[2,4,5,6]What a strong answer looks like
State your approach and its time/space complexity out loud before you optimize. Handle the edge cases (empty input, duplicates, overflow), and say why you chose this over the brute force. Green tests are the floor, not the grade.
0:00 of about 30 min
solution.py
InputExpectedGot
[[[1,2],[2,3],[5],[0],[5],[],[]]][2,4,5,6]not run yetsample