Leetcode Problem 2093. Minimum Cost to Reach City With Discounts
2093. Minimum Cost to Reach City With Discounts
Leetcode Solutions
Dijkstra's Algorithm with Discounts
Create a priority queue (min-heap) to store the state of each city (cost, discounts left, city index).
Initialize the priority queue with the starting city (0, discounts, 0).
Create a visited set to store visited states to avoid revisiting.
While the priority queue is not empty:
a. Pop the state with the minimum cost from the priority queue.
b. If the current city is the destination (n-1), return the current cost.
c. If the state has been visited with equal or more discounts, skip it.
d. For each neighboring city connected by a highway:
i. Calculate the cost without using a discount and push the new state to the queue if it's better.
ii. If discounts are available, calculate the cost with a discount and push the new state to the queue if it's better.