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 strictlyincreasing 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 modulo109 + 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.length
n == grid[i].length
1 <= m, n <= 1000
1 <= m * n <= 105
1 <= grid[i][j] <= 105
Solutions
Solution 1: DFS + Memorization
Thinking
Strictly increasing paths may start anywhere and have any length. Up to \(10^5\) cells makes enumeration impossible. Paths from a cell depend only on strictly larger neighbors.
Memoize \(dfs(i,j)\) as one (the cell itself) plus the four larger neighbors. Each cell is solved once; the sum over the grid is the answer.
We design a function \(dfs(i, j)\), which represents the number of strictly increasing paths that can be reached from the grid graph starting at the \(i\)-th row and \(j\)-th column. Then the answer is \(\sum_{i=0}^{m-1} \sum_{j=0}^{n-1} dfs(i, j)\). In the search process, we can use a two-dimensional array \(f\) to record the calculated results to avoid repeated calculation.
The calculation process of the function \(dfs(i, j)\) is as follows:
If \(f[i][j]\) is not \(0\), it means that it has been calculated, and \(f[i][j]\) is returned directly;
Otherwise, we initialize \(f[i][j] = 1\), and then enumerate the four directions of \((i, j)\). If the grid \((x, y)\) in a certain direction satisfies \(0 \leq x \lt m\), \(0 \leq y \lt n\), and \(grid[i][j] \lt grid[x][y]\), we can start from the grid \((i, j)\) to the grid \((x, y)\), and the number on the path is strictly increasing, so \(f[i][j] += dfs(x, y)\).
Finally, we return \(f[i][j]\).
The answer is \(\sum_{i=0}^{m-1} \sum_{j=0}^{n-1} dfs(i, j)\).
The time complexity is \(O(m \times n)\), and the space complexity is \(O(m \times n)\). Where \(m\) and \(n\) are the number of rows and columns in the grid graph, respectively.
Strictly increasing paths may start at any cell. The grid has up to \(10^5\) cells. Memoizing a walk toward larger neighbors follows one chain on a strictly increasing row of length \(1000\), and both \(m\) and \(n\) can be \(1000\), which exhausts the call stack.
The bottleneck is that chain: every call steps into a strictly larger neighbor that has not been finished.
The count at a cell depends only on strictly larger neighbors, so those edges form a DAG.
Sort cells by decreasing value. Set \(f[i][j]=1\), then add the finished counts of the four larger neighbors and reduce modulo \(10^9+7\). The sum of every \(f[i][j]\), reduced again, is the number of paths.
Let \(f[i][j]\) be the number of strictly increasing paths that start at row \(i\) and column \(j\). A cell contributes one path by itself, so the initial value is \(1\). It may also step to a strictly larger neighbor, which gives
A larger cell does not depend on the current one. Processing cells from largest value to smallest finishes those neighbors first. The answer is \(\sum f[i][j]\) modulo \(10^9+7\).
The time complexity is \(O(mn \log(mn))\), and the space complexity is \(O(mn)\). Here \(m\) and \(n\) are the number of rows and columns.