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.
Break down a hard problem into reliable checkpoints, edge-case handling, and complexity trade-offs.
You are given an m x n integer matrix grid, where you can move from a cell to any adjacent cell in all 4 directions.
Return the number of strictly increasing paths in the grid such that you can start from any cell and end at any cell. Since the answer may be very large, return it modulo 109 + 7.
Two paths are considered different if they do not have exactly the same sequence of visited cells.
Example 1:
Input: grid = [[1,1],[3,4]] Output: 8 Explanation: The strictly increasing paths are: - Paths with length 1: [1], [1], [3], [4]. - Paths with length 2: [1 -> 3], [1 -> 4], [3 -> 4]. - Paths with length 3: [1 -> 3 -> 4]. The total number of paths is 4 + 3 + 1 = 8.
Example 2:
Input: grid = [[1],[2]] Output: 3 Explanation: The strictly increasing paths are: - Paths with length 1: [1], [2]. - Paths with length 2: [1 -> 2]. The total number of paths is 2 + 1 = 3.
Constraints:
m == grid.lengthn == grid[i].length1 <= m, n <= 10001 <= m * n <= 1051 <= grid[i][j] <= 105Problem summary: You are given an m x n integer matrix grid, where you can move from a cell to any adjacent cell in all 4 directions. Return the number of strictly increasing paths in the grid such that you can start from any cell and end at any cell. Since the answer may be very large, return it modulo 109 + 7. Two paths are considered different if they do not have exactly the same sequence of visited cells.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Array · Dynamic Programming · Topological Sort
[[1,1],[3,4]]
[[1],[2]]
longest-increasing-path-in-a-matrix)all-paths-from-source-to-target)maximum-strictly-increasing-cells-in-a-matrix)Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #2328: Number of Increasing Paths in a Grid
class Solution {
private int[][] f;
private int[][] grid;
private int m;
private int n;
private final int mod = (int) 1e9 + 7;
public int countPaths(int[][] grid) {
m = grid.length;
n = grid[0].length;
this.grid = grid;
f = new int[m][n];
int ans = 0;
for (int i = 0; i < m; ++i) {
for (int j = 0; j < n; ++j) {
ans = (ans + dfs(i, j)) % mod;
}
}
return ans;
}
private int dfs(int i, int j) {
if (f[i][j] != 0) {
return f[i][j];
}
int ans = 1;
int[] dirs = {-1, 0, 1, 0, -1};
for (int k = 0; k < 4; ++k) {
int x = i + dirs[k], y = j + dirs[k + 1];
if (x >= 0 && x < m && y >= 0 && y < n && grid[i][j] < grid[x][y]) {
ans = (ans + dfs(x, y)) % mod;
}
}
return f[i][j] = ans;
}
}
// Accepted solution for LeetCode #2328: Number of Increasing Paths in a Grid
func countPaths(grid [][]int) (ans int) {
const mod = 1e9 + 7
m, n := len(grid), len(grid[0])
f := make([][]int, m)
for i := range f {
f[i] = make([]int, n)
}
var dfs func(int, int) int
dfs = func(i, j int) int {
if f[i][j] != 0 {
return f[i][j]
}
f[i][j] = 1
dirs := [5]int{-1, 0, 1, 0, -1}
for k := 0; k < 4; k++ {
x, y := i+dirs[k], j+dirs[k+1]
if x >= 0 && x < m && y >= 0 && y < n && grid[i][j] < grid[x][y] {
f[i][j] = (f[i][j] + dfs(x, y)) % mod
}
}
return f[i][j]
}
for i, row := range grid {
for j := range row {
ans = (ans + dfs(i, j)) % mod
}
}
return
}
# Accepted solution for LeetCode #2328: Number of Increasing Paths in a Grid
class Solution:
def countPaths(self, grid: List[List[int]]) -> int:
@cache
def dfs(i: int, j: int) -> int:
ans = 1
for a, b in pairwise((-1, 0, 1, 0, -1)):
x, y = i + a, j + b
if 0 <= x < m and 0 <= y < n and grid[i][j] < grid[x][y]:
ans = (ans + dfs(x, y)) % mod
return ans
mod = 10**9 + 7
m, n = len(grid), len(grid[0])
return sum(dfs(i, j) for i in range(m) for j in range(n)) % mod
// Accepted solution for LeetCode #2328: Number of Increasing Paths in a Grid
/**
* [2328] Number of Increasing Paths in a Grid
*
* You are given an m x n integer matrix grid, where you can move from a cell to any adjacent cell in all 4 directions.
* Return the number of strictly increasing paths in the grid such that you can start from any cell and end at any cell. Since the answer may be very large, return it modulo 10^9 + 7.
* Two paths are considered different if they do not have exactly the same sequence of visited cells.
*
* Example 1:
* <img alt="" src="https://assets.leetcode.com/uploads/2022/05/10/griddrawio-4.png" style="width: 181px; height: 121px;" />
* Input: grid = [[1,1],[3,4]]
* Output: 8
* Explanation: The strictly increasing paths are:
* - Paths with length 1: [1], [1], [3], [4].
* - Paths with length 2: [1 -> 3], [1 -> 4], [3 -> 4].
* - Paths with length 3: [1 -> 3 -> 4].
* The total number of paths is 4 + 3 + 1 = 8.
*
* Example 2:
*
* Input: grid = [[1],[2]]
* Output: 3
* Explanation: The strictly increasing paths are:
* - Paths with length 1: [1], [2].
* - Paths with length 2: [1 -> 2].
* The total number of paths is 2 + 1 = 3.
*
*
* Constraints:
*
* m == grid.length
* n == grid[i].length
* 1 <= m, n <= 1000
* 1 <= m * n <= 10^5
* 1 <= grid[i][j] <= 10^5
*
*/
pub struct Solution {}
// problem: https://leetcode.com/problems/number-of-increasing-paths-in-a-grid/
// discuss: https://leetcode.com/problems/number-of-increasing-paths-in-a-grid/discuss/?currentPage=1&orderBy=most_votes&query=
// submission codes start here
impl Solution {
pub fn count_paths(grid: Vec<Vec<i32>>) -> i32 {
0
}
}
// submission codes end
#[cfg(test)]
mod tests {
use super::*;
#[test]
#[ignore]
fn test_2328_example_1() {
let grid = vec![vec![1, 1], vec![3, 4]];
let result = 8;
assert_eq!(Solution::count_paths(grid), result);
}
#[test]
#[ignore]
fn test_2328_example_2() {
let grid = vec![vec![1], vec![2]];
let result = 3;
assert_eq!(Solution::count_paths(grid), result);
}
}
// Accepted solution for LeetCode #2328: Number of Increasing Paths in a Grid
function countPaths(grid: number[][]): number {
const mod = 1e9 + 7;
const m = grid.length;
const n = grid[0].length;
const f = new Array(m).fill(0).map(() => new Array(n).fill(0));
const dfs = (i: number, j: number): number => {
if (f[i][j]) {
return f[i][j];
}
let ans = 1;
const dirs: number[] = [-1, 0, 1, 0, -1];
for (let k = 0; k < 4; ++k) {
const x = i + dirs[k];
const y = j + dirs[k + 1];
if (x >= 0 && x < m && y >= 0 && y < n && grid[i][j] < grid[x][y]) {
ans = (ans + dfs(x, y)) % mod;
}
}
return (f[i][j] = ans);
};
let ans = 0;
for (let i = 0; i < m; ++i) {
for (let j = 0; j < n; ++j) {
ans = (ans + dfs(i, j)) % mod;
}
}
return ans;
}
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.