My first instinct was BFS and I started coding it before really thinking through the time constraint mechanics.
Model the grid as a graph where each cell's weight is its entry time, and the cost to enter a cell is max(current_time, cell_value). Use a modified Dijkstra's algorithm with a priority queue to always expand the cell with the smallest arrival time, updating neighbors accordingly. The answer is the time when the bottom-right cell is first popped.
Pro tip: Clarify that waiting is allowed and that the cost function is max(current_time, cell_value), not addition. Mention that this is a minimax path problem and can also be solved with binary search + BFS, but Dijkstra is more efficient.
Restate the problem: find the minimum time to reach (n-1, n-1) from (0,0) where you can move in 4 directions and can only enter a cell if current time >= cell's value. Note that you can wait, so time never decreases.
Define the state as (time, row, col). The cost to move from a cell with time t to a neighbor with value v is max(t, v). This is the earliest time you can be at the neighbor.
Use Dijkstra's algorithm because edge weights are non-negative and we want the minimum time to each cell. Initialize a min-heap with (grid[0][0], 0, 0) and a distance array with infinity.
While the heap is not empty, pop the cell with the smallest time. If it's the target, return the time. Otherwise, for each neighbor, compute new_time = max(current_time, grid[nr][nc]) and if it's less than the stored distance, update and push to heap.
Time complexity is O(n^2 log n) due to heap operations on up to n^2 nodes. Space complexity is O(n^2) for the distance array and heap.
AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.