MediumNo. 2093LeetCode Premium
Minimum Cost to Reach City With Discounts — LeetCode 2093 Python Solution
- Pattern
- Heap / Priority Queue
- Topics
- Graph, Shortest Path, Heap (Priority Queue)
- Reading time
- 4 min
- Source
- leetcode.com
The problem
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.
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.
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 -1Complexity
- Time
- O(n log n)
- Space · auxiliary
- O(n)
Pattern: Heap / Priority Queue
Keep only the best k elements, or always pull the smallest, in log time. LeetCode 2093. Minimum Cost to Reach City With Discounts is filed here on both counts: the reference solution below belongs to the algorithm family this hub collects, and LeetCode tags it Heap (Priority Queue).
The heap / priority queue guide has the Python template for the pattern and the 163 LeetCode problems that use it.
Related problems
| No. | Problem | Difficulty |
|---|---|---|
| 0743 | Network Delay Time | Medium |
| 0882 | Reachable Nodes In Subdivided Graph | Hard |
| 1368 | Minimum Cost to Make at Least One Valid Path in a Grid | Hard |
| 1514 | Path with Maximum Probability | Medium |
| 2290 | Minimum Obstacle Removal to Reach Corner | Hard |
| 2577 | Minimum Time to Visit a Cell In a Grid | Hard |
Frequently asked questions
- How hard is LeetCode 2093. Minimum Cost to Reach City With Discounts?
LeetCode 2093. Minimum Cost to Reach City With Discounts is rated Medium on LeetCode.
- What is the time complexity of LeetCode 2093. Minimum Cost to Reach City With Discounts?
The Python solution on this page runs in O(n log n).
- What is the space complexity of LeetCode 2093. Minimum Cost to Reach City With Discounts?
The Python solution on this page uses O(n) auxiliary space.
- What topics does LeetCode 2093. Minimum Cost to Reach City With Discounts cover?
LeetCode 2093. Minimum Cost to Reach City With Discounts is tagged Graph, Shortest Path and Heap (Priority Queue) on LeetCode.
- Is LeetCode 2093. Minimum Cost to Reach City With Discounts a premium problem?
Yes. LeetCode 2093. Minimum Cost to Reach City With Discounts is a LeetCode Premium problem, so the full statement and test cases require a paid LeetCode subscription.