Leetcode Problem 2577. Minimum Time to Visit a Cell In a Grid

2577. Minimum Time to Visit a Cell In a Grid

Leetcode Solutions

Modified Dijkstra's Algorithm

  1. Check if the cells adjacent to the starting cell are unreachable; if so, return -1.
  2. Initialize a priority queue to store the cells along with the minimum time required to reach them.
  3. Insert the starting cell (0, 0) with time 0 into the queue.
  4. While the queue is not empty: a. Pop the cell with the minimum time from the queue. b. If this cell is the bottom-right cell, return the current time. c. For each of the four adjacent cells: i. Calculate the time required to move to the adjacent cell. ii. If the calculated time is less than the time stored for the adjacent cell, update it and push the cell into the queue.
  5. If the bottom-right cell is never reached, return -1.
UML Thumbnail

Breadth-First Search (BFS) with Waiting Time Adjustment

Ask Question

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

Suggested Answer

Answer
Full Screen
Copy Answer Code
Loading...