Imagine you're exploring a mountainous terrain represented by a grid where each cell contains an elevation value. You can move to any of the 4 adjacent cells (up, down, left, right), but there's a catch - you can only move to cells with strictly higher elevation!
Given an m x n integer matrix grid, your task is to find the total number of strictly increasing paths possible in this terrain. You can start from any cell and end at any cell, as long as each step takes you to a higher elevation.
Key Points:
- Two paths are different if they visit different sequences of cells
- A single cell counts as a valid path of length 1
- Return the answer modulo
109 + 7since it can be very large
Example: In a 2x2 grid [[1,1],[3,4]], you have paths like: [1]→[3]→[4], [1]→[3], [3]→[4], and all single cells, totaling 8 different increasing paths.
Input & Output
Visualization
Time & Space Complexity
Each cell is processed exactly once due to memoization, and we visit m*n cells
Memoization table stores results for all m*n cells, plus recursion stack
Constraints
- m == grid.length
- n == grid[i].length
- 1 ≤ m, n ≤ 1000
- 1 ≤ grid[i][j] ≤ 105
- All values in the grid are unique