Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitd20b30b

Browse files
Create 2328-Number-of-Increasing-Paths-in-a-Grid.java
1 parentcfd56a1 commitd20b30b

File tree

1 file changed

+37
-0
lines changed

1 file changed

+37
-0
lines changed
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
//Exactly similar to : 329. Longest Increasing Path in a Matrix
2+
3+
classSolution {
4+
longmod = (long)Math.pow(10,9)+7;
5+
publicintcountPaths(int[][]grid) {
6+
intm =grid.length,n =grid[0].length;
7+
long[][]dp =newlong[m][n];
8+
for (inti =0;i<grid.length;i++) {
9+
for (intj =0;j<grid[0].length;j++) {
10+
if (dp[i][j]==0) {
11+
dfs(grid,dp,i,j,m,n, -1);
12+
}
13+
}
14+
}
15+
longsum =0;
16+
for (long[]d:dp) {
17+
for (longi:d) {
18+
sum= (sum+i)%mod;
19+
}
20+
}
21+
return (int)sum;
22+
}
23+
24+
publiclongdfs(int[][]grid,long[][]dp,inti,intj,intm,intn,intprevValue) {
25+
if (i<0 ||j<0 ||i>=m ||j>=n ||prevValue>=grid[i][j])
26+
return0;
27+
if (dp[i][j]!=0)
28+
returndp[i][j];
29+
prevValue =grid[i][j];
30+
longleft =dfs(grid,dp,i,j-1,m,n,prevValue)%mod;
31+
longbottom =dfs(grid,dp,i+1,j,m,n,prevValue)%mod;
32+
longright =dfs(grid,dp,i,j+1,m,n,prevValue)%mod;
33+
longtop =dfs(grid,dp,i-1,j,m,n,prevValue)%mod;
34+
dp[i][j] = (1+left+top+bottom+right)%mod;
35+
returndp[i][j];
36+
}
37+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp