The Minimum Time to Visit a Cell In a Grid problem places you at the top-left cell (0,0) of a grid where each cell has a minimum time before you can enter it. You move one step per second. Find the minimum time to reach the bottom-right cell, or return -1 if impossible. This hard Minimum Time to Visit a Cell In a Grid coding problem is a modified Dijkstra's problem where wait times create a "step back and forth" mechanic for timing alignment.
Microsoft, Atlassian, Amazon, and Google ask this hard problem because it requires recognizing that you can "waste time" by moving back and forth between adjacent cells to satisfy a cell's minimum entry time. This non-obvious mechanic transforms the problem from standard BFS to Dijkstra's with a parity-based time adjustment. The shortest path, array, matrix, BFS, graph, and heap interview pattern is the core.
Dijkstra with wait-time adjustment. Use a min-heap storing (time, row, col). When reaching a neighbor cell (r, c) at current time T, the effective entry time is max(T+1, grid[r][c]). If grid[r][c] > T+1 and the difference has wrong parity (can't reach the target time by stepping back and forth), add 1 more second. Update if the computed time is better than currently known. Check if (0,0)'s adjacent cells are reachable (if grid[0][1] > 1 and grid[1][0] > 1, return -1).
Grid: [[0,1,3,2],[5,1,2,5],[4,3,8,6]]. Start at (0,0), reach (2,3).
Grid problems with time-dependent entry constraints always need Dijkstra's (min-heap) instead of BFS. The "waste time by bouncing" mechanic is the key insight here — if you arrive early to a cell, you can oscillate until the required time, but only if the parity matches. Identifying parity constraints in time-based problems is a recurring advanced pattern. Practice Dijkstra's on grids first, then add time constraints layer by layer.
| Title | Difficulty | Topics | LeetCode |
|---|---|---|---|
| Minimum Obstacle Removal to Reach Corner | Hard | Solve | |
| Minimum Cost to Make at Least One Valid Path in a Grid | Hard | Solve | |
| Find a Safe Walk Through a Grid | Medium | Solve | |
| The Maze II | Medium | Solve | |
| Find Minimum Time to Reach Last Room II | Medium | Solve |