Longest Path in a Directed Acyclic Graph
Problem statement
You are given a directed acyclic graph with nodes 0 through n - 1 and directed edges [from, to]. Return the maximum number of edges in any directed path. An isolated node forms a path of length zero.
Function
longestDagPath(n: int, edges: int[][]) → intExamples
Example 1
n = 5edges = [[0,1],[0,2],[1,3],[2,3],[3,4]]return = 3A longest path such as 0 → 1 → 3 → 4 contains three edges.
Example 2
n = 4edges = []return = 0Every node is isolated, so the longest path has zero edges.
Constraints
1 <= n <= 1000000 <= edges.length <= 200000- The graph is acyclic and contains no duplicate edge.