Skip to content

Commit fbf0af5

Browse files
committedApr 7, 2025
Add solution #1269
1 parent e052556 commit fbf0af5

File tree

2 files changed

+39
-1
lines changed

2 files changed

+39
-1
lines changed
 

‎README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# 1,203 LeetCode solutions in JavaScript
1+
# 1,204 LeetCode solutions in JavaScript
22

33
[https://leetcodejavascript.com](https://leetcodejavascript.com)
44

@@ -964,6 +964,7 @@
964964
1263|[Minimum Moves to Move a Box to Their Target Location](./solutions/1263-minimum-moves-to-move-a-box-to-their-target-location.js)|Hard|
965965
1267|[Count Servers that Communicate](./solutions/1267-count-servers-that-communicate.js)|Medium|
966966
1268|[Search Suggestions System](./solutions/1268-search-suggestions-system.js)|Medium|
967+
1269|[Number of Ways to Stay in the Same Place After Some Steps](./solutions/1269-number-of-ways-to-stay-in-the-same-place-after-some-steps.js)|Hard|
967968
1287|[Element Appearing More Than 25% In Sorted Array](./solutions/1287-element-appearing-more-than-25-in-sorted-array.js)|Easy|
968969
1290|[Convert Binary Number in a Linked List to Integer](./solutions/1290-convert-binary-number-in-a-linked-list-to-integer.js)|Easy|
969970
1291|[Sequential Digits](./solutions/1291-sequential-digits.js)|Medium|
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/**
2+
* 1269. Number of Ways to Stay in the Same Place After Some Steps
3+
* https://leetcode.com/problems/number-of-ways-to-stay-in-the-same-place-after-some-steps/
4+
* Difficulty: Hard
5+
*
6+
* You have a pointer at index 0 in an array of size arrLen. At each step, you can move 1 position
7+
* to the left, 1 position to the right in the array, or stay in the same place (The pointer should
8+
* not be placed outside the array at any time).
9+
*
10+
* Given two integers steps and arrLen, return the number of ways such that your pointer is still
11+
* at index 0 after exactly steps steps. Since the answer may be too large, return it modulo
12+
* 109 + 7.
13+
*/
14+
15+
/**
16+
* @param {number} steps
17+
* @param {number} arrLen
18+
* @return {number}
19+
*/
20+
var numWays = function(steps, arrLen) {
21+
const MOD = 1e9 + 7;
22+
const maxPos = Math.min(steps, arrLen - 1);
23+
let dp = new Array(maxPos + 1).fill(0);
24+
dp[0] = 1;
25+
26+
for (let step = 1; step <= steps; step++) {
27+
const prev = dp;
28+
dp = new Array(maxPos + 1).fill(0);
29+
for (let pos = 0; pos <= maxPos; pos++) {
30+
dp[pos] = prev[pos];
31+
if (pos > 0) dp[pos] = (dp[pos] + prev[pos - 1]) % MOD;
32+
if (pos < maxPos) dp[pos] = (dp[pos] + prev[pos + 1]) % MOD;
33+
}
34+
}
35+
36+
return dp[0];
37+
};

0 commit comments

Comments
 (0)
Please sign in to comment.