Magicsheet logo

Minimum Cost to Reach Destination in Time

Hard
12.5%
Updated 8/1/2025

Minimum Cost to Reach Destination in Time

1. What is this problem about?

Minimum Cost to Reach Destination in Time is a graph problem where each edge has two properties: a travel time and a travel cost. Additionally, each city (node) has a "passing fee" that you must pay when you arrive at it. You are given a maximum time limit maxTime. The objective is to find the minimum total cost (sum of passing fees) to go from city 0 to city N1N-1 such that the total travel time does not exceed maxTime. If it's impossible to reach the destination within the time limit, you return -1.

2. Why is this asked in interviews?

Companies like Meta and Databricks ask this because it involves optimizing one variable (cost) while constrained by another (time). The Minimum Cost to Reach Destination in Time interview question is a variation of the constrained shortest path problem. It tests your ability to decide between Dijkstra's algorithm and Dynamic Programming, and how to effectively prune the search space when multiple paths reach the same node.

3. Algorithmic pattern used

This problem can be solved using Dijkstra's Algorithm or Dynamic Programming. In the Dijkstra approach, the state is (cost,time,city)(cost, time, city). However, since we want to minimize cost and have a hard limit on time, we can also use DP where dp[t][i]dp[t][i] is the minimum cost to reach city ii exactly at time tt. This "Array, Graph, Dynamic Programming interview pattern" is useful when one of the constraints (like time) has a small, bounded range. By iterating through time and updating reachable cities, we build the solution incrementally.

4. Example explanation

City 0 to City 1: Time = 10, Fee = 5. City 1 to City 2: Time = 10, Fee = 10. City 0 to City 2: Time = 25, Fee = 2. maxTime = 20.

  • If we go 0 -> 1 -> 2: Total Time = 20 (<= 20), Total Cost = Fee(1) + Fee(2) = 5 + 10 = 15.
  • If we go 0 -> 2: Total Time = 25 (> 20), this path is invalid. The minimum cost within the time limit is 15. Note that we also pay the fee for the starting city 0 in some versions of this problem, but typically it's fees for subsequent cities or a specified list.

5. Common mistakes candidates make

A frequent mistake in the Minimum Cost to Reach Destination in Time coding problem is using standard Dijkstra with only the city as the state. This doesn't work because a path that is more expensive might be the only one that satisfies the time constraint. Another mistake is not pruning: if you reach a city at a later time with a higher cost than a previously recorded path, you should stop exploring that branch. Some candidates also confuse the "passing fee" with "edge weight," forgetting that fees are associated with nodes.

6. Interview preparation tip

Get comfortable with "state-based DP" on graphs. If one variable (like time) is small (e.g., maxTime <= 1000), it often serves as a dimension in your DP table. This "Dynamic Programming interview pattern" allows you to convert a complex graph problem into a structured table update. Always check the constraints to decide whether Dijkstra (O(ElogV)O(E \log V)) or DP (O(T×E)O(T \times E)) is more appropriate.

Similar Questions