Minimum Cost to Reach Destination in Time - Problem

Imagine you're planning a road trip across a country with n cities numbered from 0 to n-1. All cities are connected by bi-directional roads, but here's the catch: every city charges a passing fee just to enter!

You start at city 0 and need to reach city n-1 within maxTime minutes. Your challenge is to find the minimum cost route that gets you there on time.

Input:

  • edges[i] = [xi, yi, timei] - A road between cities xi and yi taking timei minutes
  • passingFees[j] - The fee you pay when entering city j
  • maxTime - Maximum time allowed for your journey

Output: Return the minimum cost to reach the destination within the time limit, or -1 if impossible.

Note: You pay the passing fee for every city you visit, including your starting city and destination!

Input & Output

example_1.py โ€” Basic Path Selection
$ Input: maxTime = 30, edges = [[0,1,10],[1,2,10],[2,5,10],[0,3,1],[3,4,10],[4,5,15]], passingFees = [5,1,2,20,20,3]
โ€บ Output: 11
๐Ÿ’ก Note: The optimal path is 0 โ†’ 1 โ†’ 2 โ†’ 5. Total time: 10+10+10 = 30 minutes (exactly within limit). Total cost: 5+1+2+3 = 11 dollars.
example_2.py โ€” Time Constraint Forces Expensive Route
$ Input: maxTime = 29, edges = [[0,1,10],[1,2,10],[2,5,10],[0,3,1],[3,4,10],[4,5,15]], passingFees = [5,1,2,20,20,3]
โ€บ Output: 48
๐Ÿ’ก Note: With one less minute, the direct path 0โ†’1โ†’2โ†’5 takes 30 minutes (too long). Must take 0โ†’3โ†’4โ†’5: time = 1+10+15 = 26 minutes, cost = 5+20+20+3 = 48 dollars.
example_3.py โ€” Impossible Within Time Limit
$ Input: maxTime = 25, edges = [[0,1,10],[1,2,10],[2,5,10],[0,3,1],[3,4,10],[4,5,15]], passingFees = [5,1,2,20,20,3]
โ€บ Output: -1
๐Ÿ’ก Note: No path can reach the destination within 25 minutes. The fastest path takes 26 minutes (0โ†’3โ†’4โ†’5).

Visualization

Tap to expand
START$5$1$2$20$20END$310min1min10min10min15min10minOptimal Route: $5+$1+$2+$3=$11, Time=30minAlternative: $5+$20+$20+$3=$48, Time=26minโฐTime Limit: 30min
Understanding the Visualization
1
Start Your Journey
You begin at city 0, paying the entrance fee immediately. Your GPS (Dijkstra's algorithm) starts calculating optimal routes.
2
Explore Route Options
At each city, consider all connected roads. Can you afford the time for this route? Is this the cheapest way to reach the next city by this time?
3
Update Your Route Map
Keep track of the cheapest way to reach each city at each possible time. If you find a better route, update your plan.
4
Reach Destination
The first time you reach the destination city, you've found the optimal route (thanks to processing minimum costs first).
Key Takeaway
๐ŸŽฏ Key Insight: By tracking states as (city, time_used) pairs and always processing minimum cost first, we efficiently find the cheapest route that respects our time deadline!

Time & Space Complexity

Time Complexity
โฑ๏ธ
O(n!)

In worst case, we might explore all permutations of cities

n
2n
โš  Quadratic Growth
Space Complexity
O(n)

Recursion stack depth up to number of cities

n
2n
โšก Linearithmic Space

Constraints

  • 1 โ‰ค n โ‰ค 1000
  • n - 1 โ‰ค edges.length โ‰ค 1000
  • 0 โ‰ค xi, yi โ‰ค n - 1
  • 1 โ‰ค timei โ‰ค 1000
  • 1 โ‰ค passingFees[j] โ‰ค 1000
  • 0 โ‰ค maxTime โ‰ค 1000
  • The graph is connected (there exists a path from city 0 to city n-1)
Asked in
Google 35 Amazon 28 Microsoft 22 Meta 18
43.2K 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