Number of Ways to Arrive at Destination - Problem

You're navigating through a city's road network to find the optimal route from your starting point to your destination. The city consists of n intersections numbered from 0 to n-1, connected by bi-directional roads.

Given an integer n and a 2D array roads where roads[i] = [ui, vi, timei] represents a road between intersections ui and vi that takes timei minutes to travel, your goal is to determine:

  • 🎯 Find the shortest travel time from intersection 0 to intersection n-1
  • πŸ“Š Count how many different routes achieve this shortest time

Since there might be many optimal routes, return the count modulo 109 + 7.

Note: The city is fully connected (you can reach any intersection from any other), and there's at most one direct road between any two intersections.

Input & Output

example_1.py β€” Basic Graph
$ Input: n = 7, roads = [[0,6,7],[0,1,2],[1,2,3],[1,3,3],[6,3,3],[3,5,1],[6,5,1],[2,5,1],[0,4,5],[4,6,2]]
β€Ί Output: 4
πŸ’‘ Note: The shortest path from 0 to 6 takes 7 minutes. There are 4 different ways: (0β†’6), (0β†’4β†’6), (0β†’1β†’2β†’5β†’6), and (0β†’1β†’3β†’5β†’6).
example_2.py β€” Simple Path
$ Input: n = 2, roads = [[1,0,10]]
β€Ί Output: 1
πŸ’‘ Note: There's only one road connecting the intersections, so there's exactly 1 way to travel from intersection 0 to intersection 1.
example_3.py β€” Multiple Equal Paths
$ Input: n = 4, roads = [[0,1,1],[0,2,1],[1,3,1],[2,3,1]]
β€Ί Output: 2
πŸ’‘ Note: Two paths exist with the same shortest time of 2: (0β†’1β†’3) and (0β†’2β†’3). Both take exactly 2 minutes.

Visualization

Tap to expand
GPS Route Counting VisualizationSTARTABEND3 min2 min4 min4 minRoute AnalysisπŸ“ Route 1: START β†’ A β†’ END = 3 + 4 = 7 minutesπŸ“ Route 2: START β†’ B β†’ END = 2 + 4 = 6 minutes ⭐ (shortest)πŸ“Š Result: 1 optimal route found with 6 minutes travel time🎯 Algorithm tracks shortest distance AND counts equal-time routes!
Understanding the Visualization
1
Initialize GPS
Set your starting point with distance 0 and 1 way to reach it
2
Explore Roads
Use priority queue to always explore the closest unvisited intersection first
3
Update Routes
For each intersection, check if we found a faster route or another equally fast route
4
Count Optimal Paths
Accumulate the number of ways to reach each intersection optimally
Key Takeaway
🎯 Key Insight: By enhancing Dijkstra's algorithm to track path counts alongside shortest distances, we solve both problems (shortest path + counting) in one efficient pass through the graph.

Time & Space Complexity

Time Complexity
⏱️
O((E + V) log V)

Standard Dijkstra complexity: E edges, V vertices, with priority queue operations taking O(log V)

n
2n
⚑ Linearithmic
Space Complexity
O(V + E)

Space for adjacency list (E), distance and ways arrays (V), and priority queue (V)

n
2n
βœ“ Linear Space

Constraints

  • 1 ≀ n ≀ 200
  • n - 1 ≀ roads.length ≀ n Γ— (n - 1) / 2
  • roads[i].length == 3
  • 0 ≀ ui, vi ≀ n - 1
  • 1 ≀ timei ≀ 109
  • ui β‰  vi
  • All pairs (ui, vi) are unique
  • Graph is connected - you can reach any intersection from any other
Asked in
Google 45 Amazon 38 Meta 31 Microsoft 24 Uber 19
76.4K Views
Medium-High Frequency
~25 min Avg. Time
1.8K Likes
Ln 1, Col 1
Smart Actions
πŸ’‘ Explanation
AI Ready
πŸ’‘ Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen