You have a pointer at index 0 in an array of size arrLen. At each step, you can move 1 position to the left, 1 position to the right in the array or stay in the same place (The pointer should not be placed outside the array at any time).
Given two integers steps and arrLen, return the number of ways such that your pointer still at index 0 after exactly steps steps.
Since the answer may be too large, return it modulo 10^9 + 7.

public int numWays(int steps, int arrLen)

就是从0开始 每次可以向左跳一步 或者向右跳一步 或则原地不动,但是不管怎么样跳 任何时候都不能跳出界。(边界范围是0~arrLen)
现在要求你只能跳steps步 求有多少种跳法使得我们跳完这steps步之后 还能回到0点。

光这个理解题意就废了老鼻子劲了。

但是一看这个题目就是DP

use int[][] dp
dp[i][j]: i —step, j–position,
so there will be three state transit:
dp[i-1][j]: stay
dp[i-1][j+1]: move right
dp[i-1][j-1]: move left

dp[i][j] represents for the number of ways that we using i step and stopped at the position of j
dp[i][j] = dp[i-1][j] + dp[i-1][j -1] + dp[i-1][j + 1]
so we want the state from dp[0][0] to dp[step][0]

and there is another thing that we need to pay attention to:
Notice that we can only go right as far as many steps we have. For example, for 500 steps, we can only go as far as the position 500th, doesn’t matter if the arrLen is 99999999. So we use this to avoid memory/time limit. min(steps/2+1,arrLen) – because we still needs to return to the position of 0

class Solution {
    public int numWays(int steps, int arrLen) {
        int maxPosition = Math.min(steps/2+1, arrLen);
        
        long[][] dp = new long[steps + 1][maxPosition+1];
        
        dp[1][0] = 1; //stay
        dp[1][1] = 1; //move to psoition of 1 and only takes 1 step: there is only one way to do it
        
        for (int i = 2; i <= steps; i++) {
            for (int j = 0; j < maxPosition; j++) {
                dp[i][j] = (dp[i-1][j] + dp[i-1][j+1] + (j>0? dp[i-1][j-1]: 0)) % 1000000007; //why even if we define dp as long long, we still got to mod 10^9 + 7
            }
        }
        return (int)(dp[steps][0] % 1000000007);
    }
}