Skip to content

Commit 20f6cde

Browse files
authored
Create 2328. Number of Increasing Paths in a Grid
1 parent 7db3b34 commit 20f6cde

File tree

1 file changed

+33
-0
lines changed

1 file changed

+33
-0
lines changed
+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
class Solution {
2+
int mod = 1_000_000_007;
3+
int m=0,n=0;
4+
int[][] dp = null;
5+
int[][] dir = new int[][]{{1,0},{0,1},{-1,0},{0,-1}};
6+
public int countPaths(int[][] grid) {
7+
m = grid.length;
8+
n=grid[0].length;
9+
dp = new int[m][n];
10+
int res =0;
11+
for(int i=0;i<m;i++){
12+
for(int j=0;j<n;j++){
13+
res = (res + dfs(grid,i,j,-1))%mod;
14+
}
15+
}
16+
return res;
17+
}
18+
19+
private int dfs(int[][] grid,int si,int sj,int prev){
20+
if(si<0 || sj<0 || si>=m || sj>=n || grid[si][sj]<=prev){
21+
return 0;
22+
}
23+
if(dp[si][sj]>0) return dp[si][sj];
24+
int path = 1;
25+
for(int[] d:dir){
26+
path += dfs(grid,si+d[0],sj+d[1],grid[si][sj]);
27+
}
28+
29+
path%=mod;
30+
dp[si][sj] = path;
31+
return path;
32+
}
33+
}

0 commit comments

Comments
 (0)