Number of Ways to Stay in the Same Place After Some Steps LeetCode solution in JavaScript
1var numWays = function (steps, arrLen) {2let memo = Array(steps + 1)3.fill(0)4.map(() => Array(steps + 1).fill(-1)),5mod = 10 ** 9 + 7;6return dp(0, steps);78function dp(i, steps) {9if (steps === 0) return i === 0 ? 1 : 0; // found a way10if (i > steps || i < 0 || i >= arrLen) return 0; // out of bounds11if (memo[i][steps] !== -1) return memo[i][steps]; // memoized1213let moveLeft = dp(i - 1, steps - 1);14let moveRight = dp(i + 1, steps - 1);15let stay = dp(i, steps - 1);16return (memo[i][steps] = (moveLeft + moveRight + stay) % mod);17}18};