Number of Ways to Stay in the Same Place After Some Steps - Solution & Explanation
Problem Statement
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 is still at index 0 after exactly steps steps. Since the answer may be too large, return it modulo 109 + 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 <= 5001 <= arrLen <= 106
Approach Overview
Problem Overview: You start at index 0 of a 1D array. For each step you can move left, move right, or stay in place. After exactly steps moves, count how many sequences keep you at index 0. The array length limits movement, and results must be returned modulo 1e9 + 7.
Approach 1: Recursive with Memoization (Top-Down DP) (Time: O(steps * min(arrLen, steps)), Space: O(steps * min(arrLen, steps)))
Model the process as a recursive state dfs(step, pos) representing the number of ways to reach the final state from a given step and position. From each state you have three transitions: stay (pos), move left (pos - 1), or move right (pos + 1). Memoize results in a cache to avoid recomputing overlapping subproblems. A key optimization: you never need positions beyond min(arrLen - 1, steps) because you cannot travel farther than the remaining steps. This drastically reduces the state space and keeps the recursion efficient.
Approach 2: Dynamic Programming (Bottom-Up) (Time: O(steps * min(arrLen, steps)), Space: O(steps * min(arrLen, steps)) or O(min(arrLen, steps)))
Define dp[s][i] as the number of ways to reach position i after s steps. The transition mirrors the allowed moves: dp[s][i] = dp[s-1][i] + dp[s-1][i-1] + dp[s-1][i+1]. Bound the position range to maxPos = min(arrLen - 1, steps) since larger indices are unreachable. Initialize dp[0][0] = 1 and iterate step by step while applying modulo arithmetic. A rolling array optimization reduces memory to one dimension because each row depends only on the previous step. This bottom-up approach avoids recursion overhead and is typically faster in production implementations of dynamic programming.
Recommended for interviews: The bottom-up dynamic programming approach is what interviewers typically expect. It clearly demonstrates understanding of state design, transition rules, and boundary constraints. Mentioning the position bound optimization (min(arrLen, steps)) shows deeper insight into reducing the DP state space. The recursive solution with memoization still helps demonstrate how the problem naturally decomposes into overlapping subproblems before converting it into iterative DP.
Approach 1: Dynamic Programming Approach
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.
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.
Complexity
Time Complexity: O(steps * min(steps, arrLen))
Space Complexity: O(steps * min(steps, arrLen))
Approach 2: Recursive with Memoization Approach
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.
This C implementation uses recursion with memoization. The recursive function explores all directions (stay, left, right) and stores results in a cache to prevent redundant computation, ensuring efficiency.
Complexity
Time Complexity: O(steps * min(steps, arrLen))
Space Complexity: O(steps * min(steps, arrLen))
Approach 3: Memoization Search
We observe the data range of the problem and find that steps does not exceed 500, which means that we can only go to the right for up to 500 steps.
We can design a function dfs(i, j), which represents the number of schemes when we are currently at position i and the remaining steps are j. So the answer is dfs(0, steps).
The execution process of the function dfs(i, j) is as follows:
- If
i \gt jori geq arrLenori \lt 0orj \lt 0, then return0. - If
i = 0andj = 0, then the pointer has stopped in place and there are no remaining steps, so return1. - Otherwise, we can choose to move one step to the left, one step to the right, or stay still, so return
dfs(i - 1, j - 1) + dfs(i + 1, j - 1) + dfs(i, j - 1). Note the modulo operation of the answer.
During the process, we can use memoization search to avoid repeated calculations.
The time complexity is O(steps times steps), and the space complexity is O(steps times steps). Where steps is the number of steps given in the problem.
Code
Python
Java
C++
Go
TypeScript
Complexity Comparison
| Approach | Complexity |
|---|---|
| Dynamic Programming Approach | Time Complexity: O(steps * min(steps, arrLen)) Space Complexity: O(steps * min(steps, arrLen)) |
| Recursive with Memoization Approach | Time Complexity: O(steps * min(steps, arrLen)) Space Complexity: O(steps * min(steps, arrLen)) |
| Memoization Search | — |
Detailed Complexity Analysis
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Recursive with Memoization | O(steps * min(arrLen, steps)) | O(steps * min(arrLen, steps)) | When reasoning about the problem recursively or converting recursion to DP |
| Bottom-Up Dynamic Programming | O(steps * min(arrLen, steps)) | O(min(arrLen, steps)) with rolling array | Preferred for interviews and production due to predictable iteration and memory optimization |
Video Solution
Number of Ways to Stay in the Same Place After Some Steps - Leetcode 1269 - Python • NeetCodeIO • 6,969 views views
Watch 9 more video solutions →Frequently Asked Questions
Is Number of Ways to Stay in the Same Place After Some Steps easy or hard?
Number of Ways to Stay in the Same Place After Some Steps Python/Java solution
How to solve Number of Ways to Stay in the Same Place After Some Steps in O(n)?
What is the best approach for Number of Ways to Stay in the Same Place After Some Steps?
Is Number of Ways to Stay in the Same Place After Some Steps asked at Google/Amazon/Meta?
What data structure is used in Number of Ways to Stay in the Same Place After Some Steps?
What is the time complexity of Number of Ways to Stay in the Same Place After Some Steps?
Ready to solve this problem?
Practice Number of Ways to Stay in the Same Place After Some Steps with our built-in code editor and test cases.
Practice on FleetCode