Skip to content

Commit f6f5c23

Browse files
authored
Create 1269-number-of-ways-to-stay-in-the-same-place-after-some-steps.js
1 parent ab08f2b commit f6f5c23

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/**
2+
* @param {number} steps
3+
* @param {number} arrLen
4+
* @return {number}
5+
*/
6+
const numWays = function (steps, arrLen) {
7+
const MOD = 10 ** 9 + 7
8+
const memo = Array.from({ length: (steps >> 1) + 1 }, () =>
9+
Array(steps + 1).fill(-1)
10+
)
11+
return dp(0, steps)
12+
function dp(i, steps) {
13+
if (steps === 0 && i === 0) return 1
14+
if (i < 0 || i >= arrLen || steps === 0 || i > steps) return 0
15+
if (memo[i][steps] !== -1) return memo[i][steps]
16+
return (memo[i][steps] =
17+
((dp(i + 1, steps - 1) % MOD) +
18+
(dp(i - 1, steps - 1) % MOD) +
19+
(dp(i, steps - 1) % MOD)) %
20+
MOD)
21+
}
22+
}

0 commit comments

Comments
 (0)