You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Given a directed acyclic graph (DAG) of n nodes labeled from 0 to n - 1, find all possible paths from node 0 to node n - 1, and return them in any order.
5
+
6
+
The graph is given as follows: graph[i] is a list of all nodes you can visit from node i (i.e., there is a directed edge from node i to node graph[i][j]).
7
+
8
+
Example 1:
9
+
10
+
Input: graph = [[1,2],[3],[3],[]]
11
+
Output: [[0,1,3],[0,2,3]]
12
+
Explanation: There are two paths: 0 -> 1 -> 3 and 0 -> 2 -> 3.
We start at some node in a directed graph, and every turn, we walk along a directed edge of the graph. If we reach a terminal node (that is, it has no outgoing directed edges), we stop.
5
+
6
+
We define a starting node to be safe if we must eventually walk to a terminal node. More specifically, there is a natural number k, so that we must have stopped at a terminal node in less than k steps for any choice of where to walk.
7
+
8
+
Return an array containing all the safe nodes of the graph. The answer should be sorted in ascending order.
9
+
10
+
The directed graph has n nodes with labels from 0 to n - 1, where n is the length of graph. The graph is given in the following form: graph[i] is a list of labels j such that (i, j) is a directed edge of the graph, going from node i to node j.
11
+
12
+
Example 1:
13
+
14
+
Illustration of graph
15
+
Input: graph = [[1,2],[2,3],[5],[0],[5],[],[]]
16
+
Output: [2,4,5,6]
17
+
Explanation: The given graph is shown above.
18
+
19
+
Example 2:
20
+
21
+
Input: graph = [[1,2,3,4],[1,2],[3,4],[0,4],[]]
22
+
Output: [4]
23
+
24
+
Constraints:
25
+
26
+
n == graph.length
27
+
1 <= n <= 104
28
+
0 <= graph[i].legnth <= n
29
+
graph[i] is sorted in a strictly increasing order.
30
+
The graph may contain self-loops.
31
+
The number of edges in the graph will be in the range [1, 4 * 104].
0 commit comments