Skip to content

Commit d18421c

Browse files
authored
576
1 parent 74b9427 commit d18421c

File tree

1 file changed

+33
-0
lines changed

1 file changed

+33
-0
lines changed

576. Out of Boundary Paths.java

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
// Runtime 2ms
2+
// Beats 100.00%
3+
4+
5+
// Memory 42.37MB
6+
// Beats 49.60%
7+
8+
9+
class Solution {
10+
Integer[][][] dp;
11+
int mod = 1_000_000_000+7;
12+
int m, n;
13+
public int findPaths(int m, int n, int maxMove, int x, int y) {
14+
dp = new Integer[m][n][maxMove+1];
15+
this.m=m;
16+
this.n=n;
17+
return helper(maxMove, x, y);
18+
}
19+
20+
int helper(int maxMove, int x, int y){
21+
if(x<0 || x>=m || y<0 || y>=n)return 1;
22+
if(maxMove<=0)return 0;
23+
if(dp[x][y][maxMove]!=null)return dp[x][y][maxMove];
24+
int res=0;
25+
res=(res+helper(maxMove-1, x+1, y))%mod;
26+
res=(res+helper(maxMove-1, x, y-1))%mod;
27+
res=(res+helper(maxMove-1, x-1, y))%mod;
28+
res=(res+helper(maxMove-1, x, y+1))%mod;
29+
dp[x][y][maxMove]=res;
30+
return res;
31+
32+
}
33+
}

0 commit comments

Comments
 (0)