Skip to content

Latest commit

 

History

History
 
 

1269. Number of Ways to Stay in the Same Place After Some Steps

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 
 
 

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.

 

Example 1:

Input: steps = 3, arrLen = 2
Output: 4
Explanation: There are 4 differents ways to stay at index 0 after 3 steps.
Right, Left, Stay
Stay, Right, Left
Right, Stay, Left
Stay, Stay, Stay

Example 2:

Input: steps = 2, arrLen = 4
Output: 2
Explanation: There are 2 differents ways to stay at index 0 after 2 steps
Right, Left
Stay, Stay

Example 3:

Input: steps = 4, arrLen = 2
Output: 8

 

Constraints:

  • 1 <= steps <= 500
  • 1 <= arrLen <= 10^6

Related Topics:
Dynamic Programming

Solution 1. DP

Let dp[i][j] be the number of ways to reach 0 from index j when there are i steps left.

dp[i][j] = dp[i-1][j-1] + dp[i-1][j] + dp[i-1][j+1]
dp[0][0] = 1
// OJ: https://leetcode.com/problems/number-of-ways-to-stay-in-the-same-place-after-some-steps/
// Author: github.com/lzl124631x
// Time: O(SL)
// Space: O(1)
class Solution {
    typedef long long LL;
public:
    int numWays(int steps, int arrLen) {
        LL dp[2][251] = {0}, diff[3] = {-1, 0, 1}, mod = 1e9+7;
        dp[0][0] = 1;
        arrLen = min(251, arrLen);
        for (int i = 1; i <= steps; ++i) {
            for (int j = 0; j < arrLen; ++j) {
                dp[i % 2][j] = 0;
                for (int d : diff) {
                    int t = j + d;
                    if (t < 0 || t >= arrLen) continue;
                    dp[i % 2][j] = (dp[i % 2][j] + dp[(i - 1) % 2][t]) % mod;
                }
            }
        }
        return dp[steps % 2][0];
    }
};

Solution 2. DP

// OJ: https://leetcode.com/problems/number-of-ways-to-stay-in-the-same-place-after-some-steps/
// Author: github.com/lzl124631x
// Time: O(S * min(S, L))
// Space: O(min(S, L))
// Ref: https://leetcode.com/problems/number-of-ways-to-stay-in-the-same-place-after-some-steps/discuss/436392/C%2B%2B-Bottom-Up-DP
class Solution {
public:
    int numWays(int steps, int arrLen) {
        int N = min(steps / 2 + 1, arrLen);
        vector<int> a(N + 2), b(N + 2);
        a[1] = 1;
        while (steps--) {
            for (int i = 1; i <= N; ++i) b[i] = ((long)a[i - 1] + a[i] + a[i + 1]) % ((int)1e9+7);
            swap(a, b);
        }
        return a[1];
    }
};