101 Logo
onenoughtone

Code Implementation

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

Below is the implementation of the number of ways to stay in the same place after some steps:

solution.js
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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
/**
* Calculate the number of ways to stay at index 0 after exactly steps steps.
* Dynamic Programming (2D) approach.
*
* @param {number} steps - Number of steps
* @param {number} arrLen - Length of the array
* @return {number} - Number of ways
*/
function numWays(steps, arrLen) {
const MOD = 1000000007;
// Define the maximum position we need to consider
const maxPosition = Math.min(arrLen - 1, steps);
// Initialize DP array
const dp = Array(steps + 1).fill().map(() => Array(maxPosition + 1).fill(0));
// Base case: one way to be at position 0 after 0 steps
dp[0][0] = 1;
// Fill DP array
for (let i = 1; i <= steps; i++) {
for (let j = 0; j <= maxPosition; j++) {
// Stay at position j
dp[i][j] = dp[i - 1][j];
// Move right from position j-1
if (j > 0) {
dp[i][j] = (dp[i][j] + dp[i - 1][j - 1]) % MOD;
}
// Move left from position j+1
if (j < maxPosition) {
dp[i][j] = (dp[i][j] + dp[i - 1][j + 1]) % MOD;
}
}
}
return dp[steps][0];
}
/**
* Calculate the number of ways to stay at index 0 after exactly steps steps.
* Dynamic Programming (1D) approach.
*
* @param {number} steps - Number of steps
* @param {number} arrLen - Length of the array
* @return {number} - Number of ways
*/
function numWaysOptimized(steps, arrLen) {
const MOD = 1000000007;
// Define the maximum position we need to consider
const maxPosition = Math.min(arrLen - 1, steps);
// Initialize arrays for previous and current steps
let prev = Array(maxPosition + 1).fill(0);
let curr = Array(maxPosition + 1).fill(0);
// Base case: one way to be at position 0 after 0 steps
prev[0] = 1;
// Fill arrays
for (let i = 1; i <= steps; i++) {
// Reset current array
curr = Array(maxPosition + 1).fill(0);
for (let j = 0; j <= maxPosition; j++) {
// Stay at position j
curr[j] = prev[j];
// Move right from position j-1
if (j > 0) {
curr[j] = (curr[j] + prev[j - 1]) % MOD;
}
// Move left from position j+1
if (j < maxPosition) {
curr[j] = (curr[j] + prev[j + 1]) % MOD;
}
}
// Swap arrays for next step
[prev, curr] = [curr, prev];
}
return prev[0];
}
/**
* Calculate the number of ways to stay at index 0 after exactly steps steps.
* Dynamic Programming with Further Optimization approach.
*
* @param {number} steps - Number of steps
* @param {number} arrLen - Length of the array
* @return {number} - Number of ways
*/
function numWaysFurtherOptimized(steps, arrLen) {
const MOD = 1000000007;
// Define the maximum position we need to consider
const maxPosition = Math.min(arrLen - 1, Math.floor(steps / 2));
// Initialize arrays for previous and current steps
let prev = Array(maxPosition + 1).fill(0);
let curr = Array(maxPosition + 1).fill(0);
// Base case: one way to be at position 0 after 0 steps
prev[0] = 1;
// Fill arrays
for (let i = 1; i <= steps; i++) {
// Reset current array
curr = Array(maxPosition + 1).fill(0);
for (let j = 0; j <= maxPosition; j++) {
// Stay at position j
curr[j] = prev[j];
// Move right from position j-1
if (j > 0) {
curr[j] = (curr[j] + prev[j - 1]) % MOD;
}
// Move left from position j+1
if (j < maxPosition) {
curr[j] = (curr[j] + prev[j + 1]) % MOD;
}
}
// Swap arrays for next step
[prev, curr] = [curr, prev];
}
return prev[0];
}
// Test cases
console.log(numWays(3, 2)); // 4
console.log(numWays(2, 4)); // 2
console.log(numWays(4, 2)); // 8
console.log(numWaysOptimized(3, 2)); // 4
console.log(numWaysOptimized(2, 4)); // 2
console.log(numWaysOptimized(4, 2)); // 8
console.log(numWaysFurtherOptimized(3, 2)); // 4
console.log(numWaysFurtherOptimized(2, 4)); // 2
console.log(numWaysFurtherOptimized(4, 2)); // 8

Step-by-Step Explanation

Let's break down the implementation:

  1. Define Maximum Position: Determine the maximum position we need to consider, which is the minimum of arrLen - 1 and steps (or steps / 2 for further optimization).
  2. Initialize DP Array: Set up a dynamic programming array to keep track of the number of ways to be at each position after each step.
  3. Set Base Case: Define the base case: there is one way to be at position 0 after 0 steps.
  4. Fill DP Array: For each step and position, calculate the number of ways by considering three possible moves: stay, move left, or move right.
  5. Return Final Answer: Return the value in the DP array that represents the number of ways to be at position 0 after the given number of steps.
ProblemSolutionCode
101 Logo
onenoughtone