Minimum Number of Visited Cells in a Grid

You are given a 0-indexed m x n integer matrix grid. Your initial position is at the top-left cell (0, 0).

Starting from the cell (i, j), you can move to one of the following cells:

  • Cells (i, k) with j < k <= grid[i][j] + j for rightward movement.
  • Cells (k, j) with i < k <= grid[i][j] + i for downward movement.

Return the minimum number of cells you need to visit to reach the bottom-right cell (m - 1, n - 1). If there is no valid path, return -1.

Example 1
3 4 2 1
4 2 3 1
2 1 0 0
2 4 0 0
Inputgrid = [[3,4,2,1],[4,2,3,1],[2,1,0,0],[2,4,0,0]]
Output4
The image above shows one of the paths that visits exactly 4 cells.
Example 2
3 4 2 1
4 2 1 1
2 1 1 0
3 4 1 0
Inputgrid = [[3,4,2,1],[4,2,1,1],[2,1,1,0],[3,4,1,0]]
Output3
The image above shows one of the paths that visits exactly 3 cells.

Constraints

  • m == grid.length
  • n == grid[i].length
  • 1 <= m, n <= 10^5
  • 1 <= m * n <= 10^5
  • 0 <= grid[i][j] < m * n
  • grid[m - 1][n - 1] == 0

Asked at 3 companies

</>

Your Solution

(Ctrl/Cmd + Enter)

Switching Language

Loading template...

Loading...

Sign in to save your progress

AI code evaluation

Get a correctness verdict, missed edge cases, and complexity analysis of your solution.

Sign in to evaluate