Description#
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 <= 500
1 <= arrLen <= 106
Solutions#
Solution 1: 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 j$ or $i \geq arrLen$ or $i \lt 0$ or $j \lt 0$, then return $0$.
- If $i = 0$ and $j = 0$, then the pointer has stopped in place and there are no remaining steps, so return $1$.
- 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.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
| class Solution:
def numWays(self, steps: int, arrLen: int) -> int:
@cache
def dfs(i, j):
if i > j or i >= arrLen or i < 0 or j < 0:
return 0
if i == 0 and j == 0:
return 1
ans = 0
for k in range(-1, 2):
ans += dfs(i + k, j - 1)
ans %= mod
return ans
mod = 10**9 + 7
return dfs(0, steps)
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
| class Solution {
private Integer[][] f;
private int n;
public int numWays(int steps, int arrLen) {
f = new Integer[steps][steps + 1];
n = arrLen;
return dfs(0, steps);
}
private int dfs(int i, int j) {
if (i > j || i >= n || i < 0 || j < 0) {
return 0;
}
if (i == 0 && j == 0) {
return 1;
}
if (f[i][j] != null) {
return f[i][j];
}
int ans = 0;
final int mod = (int) 1e9 + 7;
for (int k = -1; k <= 1; ++k) {
ans = (ans + dfs(i + k, j - 1)) % mod;
}
return f[i][j] = ans;
}
}
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
| class Solution {
public:
int numWays(int steps, int arrLen) {
int f[steps][steps + 1];
memset(f, -1, sizeof f);
const int mod = 1e9 + 7;
function<int(int, int)> dfs = [&](int i, int j) -> int {
if (i > j || i >= arrLen || i < 0 || j < 0) {
return 0;
}
if (i == 0 && j == 0) {
return 1;
}
if (f[i][j] != -1) {
return f[i][j];
}
int ans = 0;
for (int k = -1; k <= 1; ++k) {
ans = (ans + dfs(i + k, j - 1)) % mod;
}
return f[i][j] = ans;
};
return dfs(0, steps);
}
};
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
| func numWays(steps int, arrLen int) int {
const mod int = 1e9 + 7
f := make([][]int, steps)
for i := range f {
f[i] = make([]int, steps+1)
for j := range f[i] {
f[i][j] = -1
}
}
var dfs func(i, j int) int
dfs = func(i, j int) (ans int) {
if i > j || i >= arrLen || i < 0 || j < 0 {
return 0
}
if i == 0 && j == 0 {
return 1
}
if f[i][j] != -1 {
return f[i][j]
}
for k := -1; k <= 1; k++ {
ans += dfs(i+k, j-1)
ans %= mod
}
f[i][j] = ans
return
}
return dfs(0, steps)
}
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
| function numWays(steps: number, arrLen: number): number {
const f = Array.from({ length: steps }, () => Array(steps + 1).fill(-1));
const mod = 10 ** 9 + 7;
const dfs = (i: number, j: number) => {
if (i > j || i >= arrLen || i < 0 || j < 0) {
return 0;
}
if (i == 0 && j == 0) {
return 1;
}
if (f[i][j] != -1) {
return f[i][j];
}
let ans = 0;
for (let k = -1; k <= 1; ++k) {
ans = (ans + dfs(i + k, j - 1)) % mod;
}
return (f[i][j] = ans);
};
return dfs(0, steps);
}
|