bugfree Icon
interview-course
interview-course
interview-course
interview-course
interview-course
interview-course
interview-course
interview-course

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

  1. Create a priority queue (min-heap) to store the state of each city (cost, discounts left, city index).
  2. Initialize the priority queue with the starting city (0, discounts, 0).
  3. Create a visited set to store visited states to avoid revisiting.
  4. 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.
  5. If the destination is not reachable, return -1.
UML Thumbnail

Bellman-Ford Algorithm with Discounts

Ask Question

Programming Language
image/screenshot of info(optional)
Full Screen
Loading...

Suggested Answer

Answer
Full Screen
Copy Answer Code
Loading...
bugfree Icon
OR