Skip to content

Commit 532b766

Browse files
authored
Create Number of Ways to Stay in the Same Place After Some Steps.java
1 parent 9ae580a commit 532b766

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
class Solution {
2+
3+
private static final int MOD = 1000_000_007;
4+
5+
public int numWays(int steps, int arrLen) {
6+
arrLen = Math.min(arrLen, steps);
7+
int[][] dp = new int[arrLen][steps + 1];
8+
dp[0][0] = 1;
9+
for (int i = 1; i <= steps; i++) {
10+
for (int j = arrLen - 1; j >= 0; j--) {
11+
int result = dp[j][i - 1];
12+
if (j > 0) {
13+
result = (result + dp[j - 1][i - 1]) % MOD;
14+
}
15+
if (j < arrLen - 1) {
16+
result = (result + dp[j + 1][i - 1]) % MOD;
17+
}
18+
dp[j][i] = result;
19+
}
20+
}
21+
return dp[0][steps];
22+
}
23+
}

0 commit comments

Comments
 (0)