Skip to main content

Minimum Time to Reach Destination in Directed Graph - Solution & Explanation

Practice this problem

Problem Statement

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:

  • Wait at your current node without moving, or
  • Travel along an outgoing edge from your current node if the current time t satisfies starti <= t <= endi.

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:

  • At time t = 0, take the edge (0 → 1) which is available from 0 to 1. You arrive at node 1 at time t = 1, then wait until t = 2.
  • At time t = 2, take the edge (1 → 2) which is available from 2 to 5. You arrive at node 2 at time 3.

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:

  • Wait at node 0 until time t = 1, then take the edge (0 → 2) which is available from 1 to 5. You arrive at node 2 at t = 2.
  • Wait at node 2 until time t = 4, then take the edge (2 → 3) which is available from 4 to 7. You arrive at node 3 at t = 5.

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

Explanation:

  • Since there is no outgoing edge from node 0, it is impossible to reach node 2. Hence, the output is -1.

 

Constraints:

  • 1 <= n <= 105
  • 0 <= edges.length <= 105
  • edges[i] == [ui, vi, starti, endi]
  • 0 <= ui, vi <= n - 1
  • ui != vi
  • 0 <= starti <= endi <= 109

Approach Overview

Problem Overview: You are given a directed graph where each edge represents the time required to travel between two nodes. Starting from a source node, compute the minimum time required to reach a destination node. If multiple paths exist, you must choose the one with the smallest total travel time.

Approach 1: DFS with Path Exploration (Brute Force) (Time: O(V! ) worst case, Space: O(V))

The most direct idea is to explore every possible path from the source to the destination and track the total travel time for each path. Use a depth‑first search and maintain a running sum of edge weights while marking nodes as visited to avoid cycles. Whenever you reach the destination, update the minimum time found so far. This approach works for very small graphs but quickly becomes impractical because the number of possible paths grows exponentially. It mainly serves as a conceptual baseline before applying shortest path algorithms.

Approach 2: Dijkstra’s Algorithm with Min Heap (Optimal) (Time: O((V + E) log V), Space: O(V))

This problem is a classic single‑source shortest path scenario on a weighted directed graph. The optimal solution uses Dijkstra’s algorithm with a min heap (priority queue). First build an adjacency list representing outgoing edges for each node. Maintain a distance array where dist[i] stores the shortest known time to reach node i. Push the source node into a min heap with distance 0.

At each step, pop the node with the smallest current travel time. For each outgoing edge, compute the candidate time by adding the edge weight to the current distance. If this value is smaller than the stored distance, update it and push the neighbor into the heap. Because the heap always expands the node with the smallest known distance, the first time you finalize the destination node you have the optimal answer. This approach efficiently handles large graphs and sparse edge sets.

The algorithm relies heavily on data structures commonly used in graph problems, especially adjacency lists and priority queues. The heap operations ensure efficient selection of the next closest node, which is the key idea behind many shortest path algorithms. Implementations typically use a binary heap from the heap (priority queue) toolkit.

Recommended for interviews: Interviewers expect the Dijkstra approach. Mentioning the brute‑force DFS first shows you understand the search space and why naive exploration fails. Switching to Dijkstra demonstrates knowledge of weighted graph traversal and the ability to reduce exponential search into an efficient O((V + E) log V) solution.

Solutions for this problem are being prepared.

Try solving it yourself

Detailed Complexity Analysis

ApproachTimeSpaceWhen to Use
DFS Path Exploration (Brute Force)O(V!) worst caseO(V)Only for very small graphs or conceptual understanding of all possible paths
Dijkstra’s Algorithm with Min HeapO((V + E) log V)O(V)General case for weighted directed graphs where edge weights represent travel time

Video Solution

LeetCode 3604. Minimum Time to Reach Destination in Directed Graph | Dijkstra | Graph | HeapLeet's Code309 views views

Watch 5 more video solutions →

Frequently Asked Questions

Is Minimum Time to Reach Destination in Directed Graph easy or hard?
The problem is generally rated Medium because it requires recognizing the shortest path pattern and implementing Dijkstra’s algorithm correctly. Candidates must manage adjacency lists, heap operations, and distance updates without introducing redundant processing.
Minimum Time to Reach Destination in Directed Graph Python/Java solution
Implement Dijkstra’s algorithm with a priority queue. In Python, use heapq to store (distance, node) pairs. In Java, use PriorityQueue with a custom comparator. Both versions maintain a distance array and update neighbors when a shorter path is discovered.
How to solve Minimum Time to Reach Destination in Directed Graph in O(E log V)?
Build an adjacency list for the directed graph and maintain a distance array initialized to infinity. Use a min heap that stores pairs of (current_time, node). Repeatedly pop the smallest time, relax its outgoing edges, and update neighbors if a shorter path is found. This process results in O(E log V) complexity.
What is the best approach for Minimum Time to Reach Destination in Directed Graph?
The best approach is Dijkstra’s shortest path algorithm using a min heap (priority queue). It processes nodes in increasing order of travel time and guarantees the shortest distance in weighted graphs with non‑negative edge weights. The time complexity is O((V + E) log V) with O(V) space.
Is Minimum Time to Reach Destination in Directed Graph asked at Google/Amazon/Meta?
Shortest path problems using Dijkstra’s algorithm are common in interviews at companies like Google, Amazon, and Meta. Variants involving travel time, network delay, or minimum cost paths frequently appear in coding rounds.
What data structure is used in Minimum Time to Reach Destination in Directed Graph?
The core data structure is a min heap (priority queue) used to efficiently extract the node with the smallest current distance. The graph itself is typically stored as an adjacency list for fast traversal of outgoing edges.
What is the time complexity of Minimum Time to Reach Destination in Directed Graph?
The optimal solution using Dijkstra’s algorithm runs in O((V + E) log V) time when implemented with a binary heap. Each edge relaxation may trigger a heap push operation, and each node is processed based on its shortest discovered distance.

Ready to solve this problem?

Practice Minimum Time to Reach Destination in Directed Graph with our built-in code editor and test cases.

Practice on FleetCode