Maximum Number of Moves in a Grid - Problem
Imagine you're a chess knight, but with special movement rules! You're given an m x n matrix filled with positive integers, and you can start at any cell in the first column.
Your goal is to traverse as far right as possible, but there's a catch: you can only move to cells with strictly larger values!
From any cell (row, col), you have three possible moves:
- 🔼 Up-right:
(row-1, col+1) - ➡️ Right:
(row, col+1) - 🔽 Down-right:
(row+1, col+1)
Each move must be to a cell with a strictly greater value than your current position. Return the maximum number of moves you can make before getting stuck!
Input & Output
example_1.py — Basic Grid
$
Input:
grid = [[2,4,3,5],[5,4,9,3],[3,4,2,11],[10,9,13,15]]
›
Output:
3
💡 Note:
Starting from (0,0) with value 2: move to (1,1) with value 4, then to (1,2) with value 9, then to (2,3) with value 11. Total moves: 3.
example_2.py — Single Row
$
Input:
grid = [[3,2,4],[2,1,9],[1,1,7]]
›
Output:
0
💡 Note:
No matter which starting position we choose from the first column, we cannot make any valid moves because all adjacent cells in the next column have smaller or equal values.
example_3.py — Increasing Path
$
Input:
grid = [[1,2,3,4],[2,3,4,5],[3,4,5,6]]
›
Output:
3
💡 Note:
Starting from any cell in the first column, we can move right through all columns since each column has strictly increasing values. Maximum moves = 3.
Constraints
- m == grid.length
- n == grid[i].length
- 2 ≤ m, n ≤ 1000
- 4 ≤ m * n ≤ 105
- 1 ≤ grid[i][j] ≤ 106
Visualization
Tap to expand
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code