arrLen
to the minimum of arrLen
and steps
to avoid unnecessary computation.dp
and prevDp
of size arrLen
.prevDp[0]
to 1 as the base case, representing the number of ways to be at position 0 with 0 steps.remain = 1
to steps
:
a. Reset dp
to all zeros.
b. Iterate curr
from 0 to arrLen - 1
:
i. Calculate dp[curr]
by adding the number of ways to stay in place (prevDp[curr]
), move left (prevDp[curr - 1]
if curr > 0
), and move right (prevDp[curr + 1]
if curr < arrLen - 1
).
c. Update prevDp
to be the same as dp
.dp[0]
, which represents the number of ways to be at position 0 after steps
steps.