Leetcode #2093: Minimum Cost to Reach City With Discounts
In this guide, we solve Leetcode #2093 Minimum Cost to Reach City With Discounts in Python and focus on the core idea that makes the solution efficient.
You will see the intuition, the step-by-step method, and a clean Python implementation you can use in interviews.

Problem Statement
A series of highways connect n cities numbered from 0 to n - 1. You are given a 2D integer array highways where highways[i] = [city1i, city2i, tolli] indicates that there is a highway that connects city1i and city2i, allowing a car to go from city1i to city2i and vice versa for a cost of tolli.
Quick Facts
- Difficulty: Medium
- Premium: Yes
- Tags: Graph, Shortest Path, Heap (Priority Queue)
Intuition
We need to repeatedly access the smallest or largest element as the input changes.
A heap provides fast insertions and removals while keeping order.
Approach
Push candidates into the heap as you scan, and pop when you need the best element.
Keep the heap size bounded if the problem requires a top-k structure.
Steps:
- Push candidates into a heap.
- Pop the best candidate when needed.
- Maintain heap size or invariants.
Example
Input: n = 5, highways = [[0,1,4],[2,1,3],[1,4,11],[3,2,3],[3,4,2]], discounts = 1
Output: 9
Explanation:
Go from 0 to 1 for a cost of 4.
Go from 1 to 4 and use a discount for a cost of 11 / 2 = 5.
The minimum cost to go from 0 to 4 is 4 + 5 = 9.
Python Solution
class Solution:
def minimumCost(self, n: int, highways: List[List[int]], discounts: int) -> int:
g = defaultdict(list)
for a, b, c in highways:
g[a].append((b, c))
g[b].append((a, c))
q = [(0, 0, 0)]
dist = [[inf] * (discounts + 1) for _ in range(n)]
while q:
cost, i, k = heappop(q)
if k > discounts:
continue
if i == n - 1:
return cost
if dist[i][k] > cost:
dist[i][k] = cost
for j, v in g[i]:
heappush(q, (cost + v, j, k))
heappush(q, (cost + v // 2, j, k + 1))
return -1
Complexity
The time complexity is O(n log n). The space complexity is O(n).
Edge Cases and Pitfalls
Watch for boundary values, empty inputs, and duplicate values where applicable. If the problem involves ordering or constraints, confirm the invariant is preserved at every step.
Summary
This Python solution focuses on the essential structure of the problem and keeps the implementation interview-friendly while meeting the constraints.