FastPrepLongest Path in a Directed Acyclic Graph

Longest Path in a Directed Acyclic Graph

Suno logoSuno● MediumFULLTIMEPHONE SCREEN
Learn

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[][]) → int

Examples

Example 1

n = 5edges = [[0,1],[0,2],[1,3],[2,3],[3,4]]return = 3

A longest path such as 0 → 1 → 3 → 4 contains three edges.

Example 2

n = 4edges = []return = 0

Every node is isolated, so the longest path has zero edges.

Constraints

  • 1 <= n <= 100000
  • 0 <= edges.length <= 200000
  • The graph is acyclic and contains no duplicate edge.

More Suno problems

See Suno hiring insights
public int longestDagPath(int n, int[][] edges) {
  // Write your code here.
}
n5
edges[[0,1],[0,2],[1,3],[2,3],[3,4]]
expected3
Checking account…