You are given a m × n integer matrix grid where you start at the top-left corner (0, 0) and need to reach the bottom-right corner (m-1, n-1).

From any cell (i, j), you can make strategic jumps based on the cell's value grid[i][j]:

  • Right jumps: Move to any cell (i, k) where j < k ≤ grid[i][j] + j
  • Down jumps: Move to any cell (k, j) where i < k ≤ grid[i][j] + i

Your goal is to find the minimum number of cells you need to visit to reach the destination. If no valid path exists, return -1.

Example: If you're at cell (0, 0) with value 3, you can jump right to columns 1, 2, 3 or down to rows 1, 2, 3 in the same column.

Input & Output

example_1.py — Basic Grid Traversal
$ Input: grid = [[3,4,2,1],[4,2,3,1],[2,1,0,0],[2,4,0,0]]
Output: 4
💡 Note: Starting from (0,0) with value 3, we can jump to (0,1), (0,2), (0,3), (1,0), (2,0), or (3,0). The optimal path is (0,0) → (0,2) → (3,2) → (3,3) requiring 4 cells to be visited.
example_2.py — Impossible Path
$ Input: grid = [[3,4,2,1],[4,2,1,1],[2,1,1,0],[2,4,0,0]]
Output: -1
💡 Note: From the starting position, we cannot reach the bottom-right corner due to the 0 values blocking our path. The cell (2,2) has value 1 which only allows movement to (2,3), but (2,3) has value 0 preventing further movement.
example_3.py — Single Cell
$ Input: grid = [[2]]
Output: 1
💡 Note: The grid has only one cell, so we're already at the destination. Only 1 cell needs to be visited.

Visualization

Tap to expand
Grid Navigation Strategy3START(0,0)421423GOAL0(1,3)Jump Rules:• Right: j+1 to j+grid[i][j]• Down: i+1 to i+grid[i][j]Optimal Path:1. Start (0,0) → value 32. Jump to (0,2) → value 23. Jump to (1,3) → GOAL!Total: 3 steps
Understanding the Visualization
1
Start Position
Begin at top-left corner (0,0) with step count 1
2
Explore Jumps
From each cell, calculate all valid right and down jumps based on cell value
3
BFS Traversal
Use BFS to explore cells level by level, ensuring minimum path length
4
Destination Check
Return step count when bottom-right corner is reached, or -1 if impossible
Key Takeaway
🎯 Key Insight: BFS guarantees finding the minimum path length by exploring all cells at distance k before exploring cells at distance k+1, making it the optimal approach for this shortest path problem.

Time & Space Complexity

Time Complexity
⏱️
O(m * n * (m + n))

Each cell can be visited multiple times, and from each cell we explore up to m+n jumps

n
2n
Linear Growth
Space Complexity
O(m * n)

Queue and visited set can store up to all cells in the grid

n
2n
Linearithmic Space

Constraints

  • m == grid.length
  • n == grid[i].length
  • 1 ≤ m, n ≤ 105
  • 1 ≤ m * n ≤ 105
  • 0 ≤ grid[i][j] < m * n
  • grid[m - 1][n - 1] == 0
Asked in
Google 45 Amazon 38 Meta 32 Microsoft 28
67.3K Views
Medium-High Frequency
~25 min Avg. Time
1.8K 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