Skip to content

2328. Number of Increasing Paths in a Grid 👍

  • Time: $O(mn)$
  • Space: $O(mn)$
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
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];
  }
};
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
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];
  }
}
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
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