Skip to content

Practice Minimum Number of Visited Cells in a Grid

Read the task, work through the examples, and check your approach against the constraints. Use the original LeetCode task for coding and submissions.

Problem Statement

Start at grid[0][0]. From (i,j), move right within the same row or down within the same column by any positive number of cells up to grid[i][j], staying in bounds. Return the minimum visited-cell count, including the start and destination, to reach the bottom-right cell; return -1 if unreachable.

Source: Minimum Number of Visited Cells in a Grid on LeetCode. Use the original task for its coding environment and submissions.

Examples

# Example 1

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

# Example 2

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

# Example 3

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

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

Related LeetCode Problems

Practice more problems similar to Minimum Number of Visited Cells in a Grid.

Review your practice with PhantomCodeAI

After trying the problem yourself, use the app to discuss your approach, complexity, and edge cases.