Find the fewest cells visited to reach the bottom-right cell with row and column jumps

Quick Overview

Find the fewest cells visited on a path from the top-left to the bottom-right of a grid, where each cell's value sets how far you may jump right along its row or down its column, and return -1 when no path exists. It tests grid dynamic programming and keeping range-minimum lookups fast on grids with up to 100,000 cells.

Find the fewest cells visited to reach the bottom-right cell with row and column jumps

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: hard

Interview Round: Other

You are given an `m x n` integer matrix `grid`, indexed from 0. You start at the top-left cell `(0, 0)`. From cell `(i, j)` you can make one jump to any of these cells: - a cell `(i, k)` in the same row with `j < k <= j + grid[i][j]` (a jump to the right), or - a cell `(k, j)` in the same column with `i < k <= i + grid[i][j]` (a jump down). 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`. ### Function Signature ```python def minimum_visited_cells(grid: list[list[int]]) -> int: ``` ### Rules - A jump lands only on its target cell. Cells it passes over are not visited and do not count. - The count includes both the starting cell and the bottom-right cell, so a `1 x 1` grid returns `1`. - A cell holding `0` allows no jump out of it. - A jump range that runs past the last row or column is cut at the edge of the grid. ### Constraints - `m == len(grid)` and `n == len(grid[i])` - `1 <= m, n <= 10^5` - `1 <= m * n <= 10^5` - `0 <= grid[i][j] < m * n` - `grid[0][0] > 0` when `m * n > 1` ### Examples ```text Input: grid = [[3,4,2,1],[4,2,3,1],[2,1,0,0],[2,4,0,0]] Output: 4 ``` One shortest path is `(0,0) -> (0,1) -> (3,1) -> (3,3)`: the 4 at `(0,1)` allows a jump down to row 3, and the 4 at `(3,1)` allows a jump right to column 3. A 3-cell path does not exist, because none of the cells reachable from `(0,0)` can jump straight to `(3,3)`. ```text Input: grid = [[3,4,2,1],[4,2,1,1],[2,1,1,0],[3,4,1,0]] Output: 3 ``` Jump down from `(0,0)` to `(3,0)`, then right from `(3,0)` to `(3,3)`; both cells hold 3, which is exactly the distance needed. Two cells cannot work, since `(3,3)` shares neither a row nor a column with `(0,0)`. ```text Input: grid = [[2,1,0],[1,0,0]] Output: -1 ``` The only cells that could jump to `(1,2)` are `(1,0)`, `(1,1)` and `(0,2)`. Both `(1,1)` and `(0,2)` hold 0, and the 1 at `(1,0)` only reaches `(1,1)`, so the bottom-right cell is unreachable.

Overview: Find the fewest cells visited on a path from the top-left to the bottom-right of a grid, where each cell's value sets how far you may jump right along its row or down its column, and return -1 when no path exists. It tests grid dynamic programming and keeping range-minimum lookups fast on grids with up to 100,000 cells.

Nov 24, 2026
hardSoftware EngineerOtherCoding & Algorithms
0
0

You are given an m x n integer matrix grid, indexed from 0. You start at the top-left cell (0, 0).

From cell (i, j) you can make one jump to any of these cells:

  • a cell (i, k) in the same row with j < k <= j + grid[i][j] (a jump to the right), or
  • a cell (k, j) in the same column with i < k <= i + grid[i][j] (a jump down).

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.

Function Signature

def minimum_visited_cells(grid: list[list[int]]) -> int:

Rules

  • A jump lands only on its target cell. Cells it passes over are not visited and do not count.
  • The count includes both the starting cell and the bottom-right cell, so a 1 x 1 grid returns 1 .
  • A cell holding 0 allows no jump out of it.
  • A jump range that runs past the last row or column is cut at the edge of the grid.

Constraints

  • m == len(grid) and n == len(grid[i])
  • 1 <= m, n <= 10^5
  • 1 <= m * n <= 10^5
  • 0 <= grid[i][j] < m * n
  • grid[0][0] > 0 when m * n > 1

Examples

Input: grid = [[3,4,2,1],[4,2,3,1],[2,1,0,0],[2,4,0,0]]
Output: 4

One shortest path is (0,0) -> (0,1) -> (3,1) -> (3,3): the 4 at (0,1) allows a jump down to row 3, and the 4 at (3,1) allows a jump right to column 3. A 3-cell path does not exist, because none of the cells reachable from (0,0) can jump straight to (3,3).

Input: grid = [[3,4,2,1],[4,2,1,1],[2,1,1,0],[3,4,1,0]]
Output: 3

Jump down from (0,0) to (3,0), then right from (3,0) to (3,3); both cells hold 3, which is exactly the distance needed. Two cells cannot work, since (3,3) shares neither a row nor a column with (0,0).

Input: grid = [[2,1,0],[1,0,0]]
Output: -1

The only cells that could jump to (1,2) are (1,0), (1,1) and (0,2). Both (1,1) and (0,2) hold 0, and the 1 at (1,0) only reaches (1,1), so the bottom-right cell is unreachable.