Skip to content

Commit 24362df

Browse files
committed
feat: solve No.1269,2400
1 parent e2fe5e7 commit 24362df

2 files changed

+165
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
# 1269. Number of Ways to Stay in the Same Place After Some Steps
2+
3+
- Difficulty: Hard.
4+
- Related Topics: Dynamic Programming.
5+
- Similar Questions: Number of Ways to Reach a Position After Exactly k Steps.
6+
7+
## Problem
8+
9+
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).
10+
11+
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`.
12+
13+
 
14+
Example 1:
15+
16+
```
17+
Input: steps = 3, arrLen = 2
18+
Output: 4
19+
Explanation: There are 4 differents ways to stay at index 0 after 3 steps.
20+
Right, Left, Stay
21+
Stay, Right, Left
22+
Right, Stay, Left
23+
Stay, Stay, Stay
24+
```
25+
26+
Example 2:
27+
28+
```
29+
Input: steps = 2, arrLen = 4
30+
Output: 2
31+
Explanation: There are 2 differents ways to stay at index 0 after 2 steps
32+
Right, Left
33+
Stay, Stay
34+
```
35+
36+
Example 3:
37+
38+
```
39+
Input: steps = 4, arrLen = 2
40+
Output: 8
41+
```
42+
43+
 
44+
**Constraints:**
45+
46+
47+
48+
- `1 <= steps <= 500`
49+
50+
- `1 <= arrLen <= 106`
51+
52+
53+
54+
## Solution
55+
56+
```javascript
57+
/**
58+
* @param {number} steps
59+
* @param {number} arrLen
60+
* @return {number}
61+
*/
62+
var numWays = function(steps, arrLen) {
63+
if (arrLen === 1) return 1;
64+
arrLen = Math.min(arrLen, steps);
65+
var mod = Math.pow(10, 9) + 7;
66+
var lastArr = Array(arrLen).fill(0);
67+
lastArr[0] = 1;
68+
lastArr[1] = 1;
69+
for (var i = 1; i < steps; i++) {
70+
var newArr = Array(arrLen);
71+
for (var j = 0; j < arrLen; j++) {
72+
newArr[j] = (lastArr[j] + (lastArr[j - 1] || 0) + (lastArr[j + 1] || 0)) % mod;
73+
}
74+
lastArr = newArr;
75+
}
76+
return lastArr[0];
77+
};
78+
```
79+
80+
**Explain:**
81+
82+
nope.
83+
84+
**Complexity:**
85+
86+
* Time complexity : O(n * min(n * m)).
87+
* Space complexity : O(n).
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
# 2400. Number of Ways to Reach a Position After Exactly k Steps
2+
3+
- Difficulty: Medium.
4+
- Related Topics: Math, Dynamic Programming, Combinatorics.
5+
- Similar Questions: Unique Paths, Climbing Stairs, Reach a Number, Reaching Points, Number of Ways to Stay in the Same Place After Some Steps.
6+
7+
## Problem
8+
9+
You are given two **positive** integers `startPos` and `endPos`. Initially, you are standing at position `startPos` on an **infinite** number line. With one step, you can move either one position to the left, or one position to the right.
10+
11+
Given a positive integer `k`, return **the number of **different** ways to reach the position **`endPos`** starting from **`startPos`**, such that you perform **exactly** **`k`** steps**. Since the answer may be very large, return it **modulo** `109 + 7`.
12+
13+
Two ways are considered different if the order of the steps made is not exactly the same.
14+
15+
**Note** that the number line includes negative integers.
16+
17+
 
18+
Example 1:
19+
20+
```
21+
Input: startPos = 1, endPos = 2, k = 3
22+
Output: 3
23+
Explanation: We can reach position 2 from 1 in exactly 3 steps in three ways:
24+
- 1 -> 2 -> 3 -> 2.
25+
- 1 -> 2 -> 1 -> 2.
26+
- 1 -> 0 -> 1 -> 2.
27+
It can be proven that no other way is possible, so we return 3.
28+
```
29+
30+
Example 2:
31+
32+
```
33+
Input: startPos = 2, endPos = 5, k = 10
34+
Output: 0
35+
Explanation: It is impossible to reach position 5 from position 2 in exactly 10 steps.
36+
```
37+
38+
 
39+
**Constraints:**
40+
41+
42+
43+
- `1 <= startPos, endPos, k <= 1000`
44+
45+
46+
47+
## Solution
48+
49+
```javascript
50+
/**
51+
* @param {number} startPos
52+
* @param {number} endPos
53+
* @param {number} k
54+
* @return {number}
55+
*/
56+
var numberOfWays = function(startPos, endPos, k, dp = {}) {
57+
if (startPos === endPos && k === 0) return 1;
58+
if (k === 0) return 0;
59+
if (Math.abs(startPos - endPos) > k) return 0;
60+
if (!dp[startPos]) dp[startPos] = {};
61+
if (dp[startPos][k] === undefined) {
62+
dp[startPos][k] = (
63+
numberOfWays(startPos + 1, endPos, k - 1, dp) +
64+
numberOfWays(startPos - 1, endPos, k - 1, dp)
65+
) % (Math.pow(10, 9) + 7);
66+
}
67+
return dp[startPos][k];
68+
};
69+
```
70+
71+
**Explain:**
72+
73+
nope.
74+
75+
**Complexity:**
76+
77+
* Time complexity : O(k ^ 2).
78+
* Space complexity : O(k ^ 2).

0 commit comments

Comments
 (0)