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
Check if the cells adjacent to the starting cell are unreachable; if so, return -1.
Initialize a priority queue to store the cells along with the minimum time required to reach them.
Insert the starting cell (0, 0) with time 0 into the queue.
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.
If the bottom-right cell is never reached, return -1.
Breadth-First Search (BFS) with Waiting Time Adjustment