2328. Number of Increasing Paths in a Grid LeetCode Solution

In this guide, you will get 2328. Number of Increasing Paths in a Grid LeetCode Solution with the best time and space complexity. The solution to Number of Increasing Paths in a Grid problem is provided in various programming languages like C++, Java, and Python. This will be helpful for you if you are preparing for placements, hackathons, interviews, or practice purposes. The solutions provided here are very easy to follow and include detailed explanations.

Table of Contents

  1. Problem Statement
  2. Complexity Analysis
  3. Number of Increasing Paths in a Grid solution in C++
  4. Number of Increasing Paths in a Grid solution in Java
  5. Number of Increasing Paths in a Grid solution in Python
  6. Additional Resources
2328. Number of Increasing Paths in a Grid LeetCode Solution image

Problem Statement of 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 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.length
n == grid[i].length
1 <= m, n <= 1000
1 <= m * n <= 105
1 <= grid[i][j] <= 105

Complexity Analysis

  • Time Complexity: O(mn)
  • Space Complexity: O(mn)

2328. Number of Increasing Paths in a Grid LeetCode Solution in C++

class Solution {
 public:
  int countPaths(vector<vector<int>>& grid) {
    const int m = grid.size();
    const int n = grid[0].size();
    int ans = 0;
    vector<vector<int>> mem(m, vector<int>(n, -1));

    for (int i = 0; i < m; ++i)
      for (int j = 0; j < n; ++j) {
        ans += countPaths(grid, i, j, mem);
        ans %= kMod;
      }

    return ans;
  }

 private:
  static constexpr int kMod = 1'000'000'007;
  static constexpr int dirs[4][2] = {{0, 1}, {1, 0}, {0, -1}, {-1, 0}};

  // Returns the number of increasing paths starting from (i, j).
  int countPaths(const vector<vector<int>>& grid, int i, int j,
                 vector<vector<int>>& mem) {
    if (mem[i][j] != -1)
      return mem[i][j];

    mem[i][j] = 1;  // The current cell contributes 1 length.

    for (const auto& [dx, dy] : dirs) {
      const int x = i + dx;
      const int y = j + dy;
      if (x < 0 || x == grid.size() || y < 0 || y == grid[0].size())
        continue;
      if (grid[x][y] <= grid[i][j])
        continue;
      mem[i][j] += countPaths(grid, x, y, mem);
      mem[i][j] %= kMod;
    }

    return mem[i][j];
  }
};
/* code provided by PROGIEZ */

2328. Number of Increasing Paths in a Grid LeetCode Solution in Java

class Solution {
  public int countPaths(int[][] grid) {
    final int m = grid.length;
    final int n = grid[0].length;
    int ans = 0;
    Integer[][] mem = new Integer[m][n];

    for (int i = 0; i < m; ++i)
      for (int j = 0; j < n; ++j) {
        ans += countPaths(grid, i, j, mem);
        ans %= kMod;
      }

    return ans;
  }

  private static final int kMod = 1_000_000_007;
  private static final int[][] dirs = {{0, 1}, {1, 0}, {0, -1}, {-1, 0}};

  // Returns the number of increasing paths starting from (i, j).
  private int countPaths(int[][] grid, int i, int j, Integer[][] mem) {
    if (mem[i][j] != null)
      return mem[i][j];

    mem[i][j] = 1; // The current cell contributes 1 length.

    for (int[] dir : dirs) {
      int x = i + dir[0];
      int y = j + dir[1];
      if (x < 0 || x == grid.length || y < 0 || y == grid[0].length)
        continue;
      if (grid[x][y] <= grid[i][j])
        continue;
      mem[i][j] += countPaths(grid, x, y, mem);
      mem[i][j] %= kMod;
    }

    return mem[i][j];
  }
}
// code provided by PROGIEZ

2328. Number of Increasing Paths in a Grid LeetCode Solution in Python

class Solution:
  def countPaths(self, grid: list[list[int]]) -> int:
    kMod = 1_000_000_007
    dirs = ((0, 1), (1, 0), (0, -1), (-1, 0))
    m = len(grid)
    n = len(grid[0])

    @functools.lru_cache(None)
    def dp(i: int, j: int) -> int:
      """Returns the number of increasing paths starting from (i, j)."""
      ans = 1  # The current cell contributes 1 length.
      for dx, dy in dirs:
        x = i + dx
        y = j + dy
        if x < 0 or x == m or y < 0 or y == n:
          continue
        if grid[x][y] <= grid[i][j]:
          continue
        ans += dp(x, y)
        ans %= kMod
      return ans

    return sum(dp(i, j)
               for i in range(m)
               for j in range(n)) % kMod
# code by PROGIEZ

Additional Resources

Happy Coding! Keep following PROGIEZ for more updates and solutions.