Minimum Time to Visit a Cell In a Grid - Problem

You are given an m x n matrix grid consisting of non-negative integers where grid[row][col] represents the minimum time required to be able to visit the cell (row, col), which means you can visit the cell (row, col) only when the time you visit it is greater than or equal to grid[row][col].

You are standing in the top-left cell of the matrix in the 0th second, and you must move to any adjacent cell in the four directions: up, down, left, and right. Each move you make takes 1 second.

Return the minimum time required in which you can visit the bottom-right cell of the matrix. If you cannot visit the bottom-right cell, then return -1.

Input & Output

Example 1 — Basic Grid Navigation
$ Input: grid = [[0,1,1],[2,3,1],[2,3,1]]
Output: 4
💡 Note: Start at (0,0) at time 0, move right to (0,1) at time 1, then right to (0,2) at time 2, then down to (1,2) at time 3, finally down to (2,2) at time 4. Total time: 4.
Example 2 — Impossible Case
$ Input: grid = [[0,2],[2,2]]
Output: -1
💡 Note: Both adjacent cells (0,1) and (1,0) require time ≥2 to enter, but we can only reach them at time 1. Since we can't move anywhere from start, return -1.
Example 3 — Waiting Strategy
$ Input: grid = [[0,0,0],[0,0,5],[0,0,0]]
Output: 5
💡 Note: We can reach (1,2) by going around: (0,0) → (0,1) → (0,2) → (1,2), arriving at time 3, but cell requires time ≥5. We can wait by moving back and forth until time 5.

Constraints

  • m == grid.length
  • n == grid[i].length
  • 2 ≤ m, n ≤ 1000
  • 0 ≤ grid[i][j] ≤ 105

Visualization

Tap to expand
Minimum Time to Visit a Cell In a Grid INPUT 3x3 Grid (minimum time to visit) 0 1 1 2 3 1 2 3 1 S E Start (0,0) at t=0 End (2,2) - Target Input Values: grid = [[0,1,1], [2,3,1], [2,3,1]] ALGORITHM STEPS 1 Initialize Priority Queue PQ = [(time=0, row=0, col=0)] dist[0][0] = 0, others = INF 2 Pop Min Time Cell Extract cell with smallest time If target reached, return time 3 Explore Neighbors For each adjacent cell (4 dirs): Calculate arrival time 4 Update and Push If new time better, update dist Push to PQ, repeat step 2 Time Calculation: new_t = max(curr_t+1, grid[nr][nc]) Wait if needed (back-and-forth) Time: O(mn log(mn)) Space: O(mn) Min-Heap PQ: [(t, row, col), ...] FINAL RESULT Optimal Path (time=4) 0 t=0 1 t=1 1 2 t=2 3 t=3 1 2 3 1 t=4 Path Sequence: (0,0) t=0 --> (0,1) t=1 (1,0) t=2 --> (1,1) t=3 (1,1) t=3 --> (2,2) t=4 OUTPUT 4 OK - Minimum time found! Key Insight: Dijkstra's algorithm finds shortest path considering time constraints. When a cell requires waiting (grid[r][c] > current_time + 1), we can move back-and-forth to "waste" time. The waiting adjustment ensures we arrive at exactly the right moment. If grid[0][1] and grid[1][0] both exceed 1, return -1. TutorialsPoint - Minimum Time to Visit a Cell In a Grid | Dijkstra's Algorithm with Priority Queue
Asked in
Google 45 Amazon 38 Meta 32 Microsoft 28
23.5K Views
Medium Frequency
~35 min Avg. Time
892 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen