mediumGraphHeap Priority QueueShortest Path 0 views

Minimum Time to Reach Destination in Directed Graph

You are given an integer n and a directed graph with n nodes labeled from 0 to n - 1.

You are given an integer n and a directed graph with n nodes labeled from 0 to n - 1. This is represented by a 2D array edges, where edges[i] = [ui, vi, starti, endi] indicates an edge from node ui to vi that can only be used at any integer time t such that starti <= t <= endi.

You start at node 0 at time 0.

In one unit of time, you can either:

Return the minimum time required to reach node n - 1. If it is impossible, return -1.

Example 1

Input: n = 3, edges = [[0,1,0,1],[1,2,2,5]]

Output: 3

Explanation: The optimal path is: Hence, the minimum time to reach node 2 is 3.

Example 2

Input: n = 4, edges = [[0,1,0,3],[1,3,7,8],[0,2,1,5],[2,3,4,7]]

Output: 5

Explanation: The optimal path is: Hence, the minimum time to reach node 3 is 5.

Example 3

Input: n = 3, edges = [[1,0,1,3],[1,2,3,5]]

Output: -1

Constraints

  • 1 <= n <= 10^5
  • 0 <= edges.length <= 10^5
  • edges[i] == [ui, vi, starti, endi]
  • 0 <= ui, vi <= n - 1
  • ui != vi
  • 0 <= starti <= endi <= 10^9

Hints

Companies

No companies reported yet.

Discussion

Sign in to join the discussion.

Loading discussion...

Test results

No test cases yet.