Tap here to switch tabs
Problem
Submissions

Problem: Find Eventual Safe States

Try to solve the Find Eventual Safe States problem.

Statement

There is a directed graph of n nodes labeled from 00 to n −1- 1. The graph is represented by a 00 indexed 22D integer array graph, where graph[i] is an integer array of nodes adjacent to node i, meaning there is a directed edge from node i to each node in graph[i].

A node is considered a terminal node if it has no outgoing edges. A node is a safe node if every possible path starting from that node leads to a terminal node.

Return an array containing all the safe nodes of the graph, sorted in ascending order.

Constraints:

  • n ==== graph.length

  • 1≤1 \leq n ≤104\leq 10^4

  • 0≤0 \leq graph[i].length ≤\leq n

  • 0≤0 \leq graph[i][j] ≤\leq n −1- 1

  • graph[i] is sorted in strictly increasing order.

  • The graph may contain self loops.

  • The total number of edges in the graph is in the range [1,4×104][1, 4 \times 10^4].

⋮
Tap here to switch tabs
Problem
Submissions

Problem: Find Eventual Safe States

Try to solve the Find Eventual Safe States problem.

Statement

There is a directed graph of n nodes labeled from 00 to n −1- 1. The graph is represented by a 00 indexed 22D integer array graph, where graph[i] is an integer array of nodes adjacent to node i, meaning there is a directed edge from node i to each node in graph[i].

A node is considered a terminal node if it has no outgoing edges. A node is a safe node if every possible path starting from that node leads to a terminal node.

Return an array containing all the safe nodes of the graph, sorted in ascending order.

Constraints:

  • n ==== graph.length

  • 1≤1 \leq n ≤104\leq 10^4

  • 0≤0 \leq graph[i].length ≤\leq n

  • 0≤0 \leq graph[i][j] ≤\leq n −1- 1

  • graph[i] is sorted in strictly increasing order.

  • The graph may contain self loops.

  • The total number of edges in the graph is in the range [1,4×104][1, 4 \times 10^4].