Loading...
You are given a directed graph on n nodes labelled 0 to n - 1, as a 0-indexed adjacency list graph: there is an edge from node i to every node listed in graph[i].
A node with no outgoing edges is a dead end. A node is settled when every path starting from it eventually stops at a dead end, equivalently when no path from it ever revisits a node, so it can never reach a cycle.
Return the labels of all settled nodes, sorted in ascending order.
The graph may contain self-loops, and a self-loop is a cycle of length one.
Input: graph = [[1,2],[2,3],[5],[0],[5],[],[]]
Output: [2,4,5,6]
Explanation: Nodes 5 and 6 are dead ends, so they are settled. Node 2 leads only to 5,
and node 4 leads only to 5, so both are settled. Nodes 0, 1 and 3 all sit on or feed the
cycle 0 -> 1 -> 3 -> 0, so none of them is.
Input: graph = [[1,2,3,4],[1,2],[3,4],[0,4],[]]
Output: [4]
Explanation: Node 4 is a dead end. Node 1 has a self-loop, and nodes 0, 2 and 3 all
reach a cycle, so 4 is the only settled node.
n = graph.lengthn ≤104graph[i].length ≤ ngraph[i][j] ≤ n −1graph[i] is sorted in strictly increasing order.Click "Run" to test with sample cases or "Submit" to run all tests.