




Sponsored
Sponsored
This approach leverages dynamic programming to find the number of ways to stay at index 0 after a given number of steps. We define a 2D table dp[i][j] where i represents the number of steps remaining, and j represents the current position of the pointer.
To optimize computation, we can limit the table size to the minimum of steps and arrLen since going beyond these positions is unnecessary.
Time Complexity: O(steps * min(steps, arrLen))
Space Complexity: O(steps * min(steps, arrLen))
1#include <stdio.h>
2#define MOD 1000000007
3
4int numWays(int steps, int arrLen) {
5    int maxPos = (arrLen < steps) ? arrLen - 1 : steps;
6    int dp[steps+1][maxPos+1];
7    for(int i = 0; i <= steps; i++)
8        for(int j = 0; j <= maxPos; j++)
9            dp[i][j] = 0;
10    
11    dp[0][0] = 1;
12    for (int i = 1; i <= steps; i++) {
13        for (int j = 0; j <= maxPos; j++) {
14            dp[i][j] = dp[i-1][j];
15            if (j > 0)
16                dp[i][j] = (dp[i][j] + dp[i-1][j-1]) % MOD;
17            if (j < maxPos)
18                dp[i][j] = (dp[i][j] + dp[i-1][j+1]) % MOD;
19        }
20    }
21    return dp[steps][0];
22}
23
24int main() {
25    printf("%d\n", numWays(3, 2)); // Output: 4
26    return 0;
27}This code implements a dynamic programming solution in C. It initializes a DP table where dp[i][j] represents the number of ways to be at position j after i steps. The transition involves considering staying at the same position, moving left, or moving right.
This approach utilizes recursion combined with memoization to optimize the recursive calls. Here, recursion is used to explore all possible paths dynamically adjusting by staying at, moving left, or moving right from each position in every step.
The results of the recursive calls are stored in a memoization table to avoid redundant calculations.
Time Complexity: O(steps * min(steps, arrLen))
Space Complexity: O(steps * min(steps, arrLen))
using System.Collections.Generic;
class NumWaysMemo
{
    const int MOD = 1000000007;
    private Dictionary<string, int> cache = new Dictionary<string, int>();
    public int NumWaysHelper(int steps, int pos, int maxPos)
    {
        if (pos < 0 || pos > maxPos) return 0;
        if (steps == 0) return pos == 0 ? 1 : 0;
        string key = steps + "," + pos;
        if (cache.ContainsKey(key)) return cache[key];
        int stay = NumWaysHelper(steps - 1, pos, maxPos);
        int left = NumWaysHelper(steps - 1, pos - 1, maxPos);
        int right = NumWaysHelper(steps - 1, pos + 1, maxPos);
        int result = ((stay + left) % MOD + right) % MOD;
        cache[key] = result;
        return result;
    }
    public int NumWays(int steps, int arrLen)
    {
        int maxPos = Math.Min(steps, arrLen - 1);
        return NumWaysHelper(steps, 0, maxPos);
    }
    static void Main(string[] args)
    {
        NumWaysMemo nwm = new NumWaysMemo();
        Console.WriteLine(nwm.NumWays(3, 2)); // Output: 4
    }
}A C# recursive solution with memoization. It pursues all move directions while caching results to eliminate redundant operations and effectively manage state transitions in an optimal manner.