Leetcode Problem 818. Race Car

818. Race Car

Leetcode Solutions

Approach #: Dijkstra's Algorithm

  1. Initialize a priority queue with the starting position (0) and speed (+1), with a cost of 0.
  2. While the priority queue is not empty, extract the node with the lowest cost.
  3. If the current node's position is the target, return the cost.
  4. Otherwise, generate the next possible nodes by applying either an 'A' or 'R' instruction.
  5. Calculate the cost for each new node and add it to the priority queue if it hasn't been visited or if the new cost is lower.
  6. Repeat steps 2-5 until the target is reached.
UML Thumbnail

Approach #: Dynamic Programming

Ask Question

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

Suggested Answer

Answer
Full Screen
Copy Answer Code
Loading...