Magicsheet logo

Minimum Cost to Reach City With Discounts

Medium
81.5%
Updated 6/1/2025

Minimum Cost to Reach City With Discounts

1. What is this problem about?

The Minimum Cost to Reach City With Discounts problem presents a graph where cities are nodes and roads are weighted edges representing the travel cost. You are also given a fixed number of "discounts." A discount allows you to traverse any single road at half its normal cost (using integer division). The goal is to find the minimum total cost to travel from city 0 to city N1N-1 while using at most the given number of discounts.

2. Why is this asked in interviews?

This problem is frequently asked by companies like Goldman Sachs because it is a perfect example of a "state-extended" shortest path problem. The Minimum Cost to Reach City With Discounts interview question evaluates whether you can modify standard algorithms like Dijkstra to handle an additional dimension of state (the number of discounts remaining). It tests both graph theory knowledge and the ability to manage state in dynamic environments.

3. Algorithmic pattern used

The algorithmic pattern is Dijkstra's Algorithm with an augmented state. Instead of just tracking the minimum cost to reach a city uu, we track the minimum cost to reach city uu with kk discounts already used. The state in our Priority Queue becomes (cost,city,discounts_used)(cost, city, discounts\_used). When exploring an neighbor vv of city uu, we have two options:

  1. Travel to vv without using a discount (if cost+weight<dist[v][discounts_used]cost + weight < dist[v][discounts\_used]).
  2. Travel to vv using a discount (if discounts_used<max_discountsdiscounts\_used < max\_discounts and cost+weight/2<dist[v][discounts_used+1]cost + weight/2 < dist[v][discounts\_used + 1]). This "Shortest Path, Graph, Heap interview pattern" is the gold standard for solving navigation problems with constraints.

4. Example explanation

Imagine city 0 to city 1 costs 10, and city 1 to city 2 costs 20. You have 1 discount.

  • Path A: 0 -> 1 (no discount, cost 10), 1 -> 2 (use discount, cost 20/2=1020/2=10). Total = 20.
  • Path B: 0 -> 1 (use discount, cost 10/2=510/2=5), 1 -> 2 (no discount, cost 20). Total = 25. The minimum cost is 20. Even though 20 is more than 10, using the discount on the more expensive road is more beneficial. Dijkstra will explore these states and find that 20 is the minimum.

5. Common mistakes candidates make

In the Minimum Cost to Reach City With Discounts coding problem, candidates often try to use a simple greedy approach (e.g., just use discounts on the most expensive edges found by standard Dijkstra). This fails because the path itself might change based on discount usage. Another mistake is not including the discount count in the "visited" or "distance" array, which leads to incorrect optimizations where a cheaper path with no discounts blocks a slightly more expensive path that has more discounts available.

6. Interview preparation tip

When a shortest path problem has a "bonus" or a "limited resource," always add that resource to your state. Practice "Dijkstra with state" as it appears in many variations, such as "shortest path with K stops" or "shortest path with refueling." This "Graph interview pattern" is essential for mastering hard-level algorithmic challenges in top-tier tech interviews.

Similar Questions