Off-by-one on range boundaries
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Move from brute-force thinking to an efficient approach using array strategy.
You are given a 0-indexed m x n matrix grid consisting of positive integers.
You can start at any cell in the first column of the matrix, and traverse the grid in the following way:
(row, col), you can move to any of the cells: (row - 1, col + 1), (row, col + 1) and (row + 1, col + 1) such that the value of the cell you move to, should be strictly bigger than the value of the current cell.Return the maximum number of moves that you can perform.
Example 1:
Input: grid = [[2,4,3,5],[5,4,9,3],[3,4,2,11],[10,9,13,15]] Output: 3 Explanation: We can start at the cell (0, 0) and make the following moves: - (0, 0) -> (0, 1). - (0, 1) -> (1, 2). - (1, 2) -> (2, 3). It can be shown that it is the maximum number of moves that can be made.
Example 2:
Input: grid = [[3,2,4],[2,1,9],[1,1,7]] Output: 0 Explanation: Starting from any cell in the first column we cannot perform any moves.
Constraints:
m == grid.lengthn == grid[i].length2 <= m, n <= 10004 <= m * n <= 1051 <= grid[i][j] <= 106Problem summary: You are given a 0-indexed m x n matrix grid consisting of positive integers. You can start at any cell in the first column of the matrix, and traverse the grid in the following way: From a cell (row, col), you can move to any of the cells: (row - 1, col + 1), (row, col + 1) and (row + 1, col + 1) such that the value of the cell you move to, should be strictly bigger than the value of the current cell. Return the maximum number of moves that you can perform.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Array · Dynamic Programming
[[2,4,3,5],[5,4,9,3],[3,4,2,11],[10,9,13,15]]
[[3,2,4],[2,1,9],[1,1,7]]
Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #2684: Maximum Number of Moves in a Grid
class Solution {
public int maxMoves(int[][] grid) {
int m = grid.length, n = grid[0].length;
Set<Integer> q = IntStream.range(0, m).boxed().collect(Collectors.toSet());
for (int j = 0; j < n - 1; ++j) {
Set<Integer> t = new HashSet<>();
for (int i : q) {
for (int k = i - 1; k <= i + 1; ++k) {
if (k >= 0 && k < m && grid[i][j] < grid[k][j + 1]) {
t.add(k);
}
}
}
if (t.isEmpty()) {
return j;
}
q = t;
}
return n - 1;
}
}
// Accepted solution for LeetCode #2684: Maximum Number of Moves in a Grid
func maxMoves(grid [][]int) (ans int) {
m, n := len(grid), len(grid[0])
q := map[int]bool{}
for i := range grid {
q[i] = true
}
for j := 0; j < n-1; j++ {
t := map[int]bool{}
for i := range q {
for k := i - 1; k <= i+1; k++ {
if k >= 0 && k < m && grid[i][j] < grid[k][j+1] {
t[k] = true
}
}
}
if len(t) == 0 {
return j
}
q = t
}
return n - 1
}
# Accepted solution for LeetCode #2684: Maximum Number of Moves in a Grid
class Solution:
def maxMoves(self, grid: List[List[int]]) -> int:
m, n = len(grid), len(grid[0])
q = set(range(m))
for j in range(n - 1):
t = set()
for i in q:
for k in range(i - 1, i + 2):
if 0 <= k < m and grid[i][j] < grid[k][j + 1]:
t.add(k)
if not t:
return j
q = t
return n - 1
// Accepted solution for LeetCode #2684: Maximum Number of Moves in a Grid
/**
* [2684] Maximum Number of Moves in a Grid
*/
pub struct Solution {}
// submission codes start here
impl Solution {
pub fn max_moves(grid: Vec<Vec<i32>>) -> i32 {
let m = grid.len();
let n = grid[0].len();
let mut dp = vec![vec![0; n]; m];
for j in (0..n - 1).rev() {
for i in 0..m {
// [row - 1, col + 1]
if i != 0 {
if grid[i - 1][j + 1] > grid[i][j] {
dp[i][j] = dp[i][j].max(dp[i - 1][j + 1] + 1);
}
}
// [row, col + 1]
if grid[i][j + 1] > grid[i][j] {
dp[i][j] = dp[i][j].max(dp[i][j + 1] + 1);
}
// [row + 1, col + 1]
if i != m - 1 {
if grid[i + 1][j + 1] > grid[i][j] {
dp[i][j] = dp[i][j].max(dp[i + 1][j + 1] + 1);
}
}
}
}
let mut result = 0;
for i in 0..m {
result = result.max(dp[i][0]);
}
result
}
}
// submission codes end
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_2684() {
assert_eq!(
3,
Solution::max_moves(vec![
vec![2, 4, 3, 5],
vec![5, 4, 9, 3],
vec![3, 4, 2, 11],
vec![10, 9, 13, 15]
])
);
assert_eq!(
0,
Solution::max_moves(vec![vec![3, 2, 4], vec![2, 1, 9], vec![1, 1, 7]])
);
}
}
// Accepted solution for LeetCode #2684: Maximum Number of Moves in a Grid
function maxMoves(grid: number[][]): number {
const m = grid.length;
const n = grid[0].length;
let q = new Set<number>(Array.from({ length: m }, (_, i) => i));
for (let j = 0; j < n - 1; ++j) {
const t = new Set<number>();
for (const i of q) {
for (let k = i - 1; k <= i + 1; ++k) {
if (k >= 0 && k < m && grid[i][j] < grid[k][j + 1]) {
t.add(k);
}
}
}
if (t.size === 0) {
return j;
}
q = t;
}
return n - 1;
}
Use this to step through a reusable interview workflow for this problem.
Pure recursion explores every possible choice at each step. With two choices per state (take or skip), the decision tree has 2ⁿ leaves. The recursion stack uses O(n) space. Many subproblems are recomputed exponentially many times.
Each cell in the DP table is computed exactly once from previously solved subproblems. The table dimensions determine both time and space. Look for the state variables — each unique combination of state values is one cell. Often a rolling array can reduce space by one dimension.
Review these before coding to avoid predictable interview regressions.
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Wrong move: An incomplete state merges distinct subproblems and caches incorrect answers.
Usually fails on: Correctness breaks on cases that differ only in hidden state.
Fix: Define state so each unique subproblem maps to one DP cell.