Skip to content

Commit ac4bba5

Browse files
committed
Update climbing stairs problem
1 parent 6590054 commit ac4bba5

File tree

6 files changed

+107
-13
lines changed

6 files changed

+107
-13
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ List of Programs related to data structures and algorithms
7171

7272
### Dynamic programming
7373

74-
1. Climbing stairs: [JavaScript](https://livecodes.io/?console&x=https://github.com/sudheerj/datastructures-algorithms/blob/master/src/javascript/algorithms/dynamicProgramming/climbingStairs.js)
74+
1. Climbing stairs: [JavaScript](https://livecodes.io/?console&x=https://github.com/sudheerj/datastructures-algorithms/blob/master/src/javascript/algorithms/dynamicProgramming/1.climbingStairs/climbingStairs.js)
7575

7676
2. Coin change: [JavaScript](https://livecodes.io/?console&x=https://github.com/sudheerj/datastructures-algorithms/blob/master/src/javascript/algorithms/dynamicProgramming/coinsChange.js)
7777

src/java1/algorithms/dynamicProgramming/ClimbingStairs.java renamed to src/java1/algorithms/dynamicProgramming/climbingStairs/ClimbingStairs.java

+7-6
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
package java1.algorithms.dynamicProgramming;
1+
package java1.algorithms.dynamicProgramming.climbingStairs;
22

33
public class ClimbingStairs {
44
// TC: O(n) SC: O(1)
55
private static int climbStairs1(int n) {
6-
if(n == 1 || n == 2) return 1;
6+
if(n <= 2) return n;
77

8-
int first = 1, second = 1, temp;
9-
for(int i=0; i< n-1; i++) {
8+
int first = 1, second = 2, temp;
9+
for(int i=2; i< n; i++) {
1010
temp = first + second;
1111
first = second;
1212
second = temp;
@@ -16,10 +16,11 @@ private static int climbStairs1(int n) {
1616

1717
// TC:O(n) SC: O(n)
1818
private static int climbStairs2(int n) {
19-
if(n == 1 || n == 2) return 1;
19+
if(n <= 2) return n;
2020

2121
int[] dp = new int[n+1];
22-
dp[0] = dp[1] = 1;
22+
dp[0] = 1;
23+
dp[1] = 1;
2324
for(int i=2; i<n+1; i++) {
2425
dp[i] = dp[i-1] + dp[i-2];
2526
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
**Description:**
2+
You are required to climb a staircase that consists of `n` individual steps. Determine the total number of distinct ways to reach the top of the staircase.
3+
4+
**Note:** Each time you can either climb 1 or 2 steps.
5+
6+
## Examples:
7+
Example 1:
8+
9+
Input: n = 5
10+
Output: 8
11+
12+
Example 2:
13+
14+
Input: n = 6
15+
Output: 13
16+
17+
**Algorithmic Steps(Approach 1&2)**
18+
This problem is solved efficiently using **dynamic programming** approach by avoiding the recomputations of same subproblems. The algorithmic approach can be summarized as follows:
19+
20+
1. Add a preliminary check like if the step value is less than or equal to 2, return the possible ways to reach them as step's value.
21+
22+
2. Create two variables(`first` and `second`) to store the possible ways to reach the current step and the next step. Since the first step takes one possible way and second step takes two possible ways, those varaibles needs to be initialized to 1 & 2 respectively.
23+
24+
3. Iterate `n-1` times to calculate the next step until `n`th step is reached.
25+
26+
4. The number of possible ways to reach next step is the combination of previous two steps possibilities. Because any particular step is reached either through 1 step or 2 steps.
27+
28+
5. Calculate the sum of first and second pointers in a temporary variable(`temp`). Assign the second pointer variable to first and update the second pointer variable to temporary variable.
29+
30+
6. Repeat steps 4-5 until the nth step is reached.
31+
32+
7. Return second pointer value which indicates the possible ways to reach top stair.
33+
34+
35+
**Time and Space complexity:**
36+
This algorithm has a time complexity of `O(n)`, where `n` is the number of steps. This is because we are traversing all the steps to calculate the possible ways for nth step.
37+
38+
Here, we don't use any additional datastructure other than two variables. Hence, the space complexity will be `O(1)`.

src/javascript/algorithms/dynamicProgramming/climbingStairs.js renamed to src/javascript/algorithms/dynamicProgramming/1.climbingStairs/climbingStairs.js

+6-6
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,20 @@
1-
// TC: O(n) SC: O(1)
1+
// Optimized fibonacci series- TC: O(n) SC: O(1)
22
function climbStairs1(n) {
3-
if(n ===1 || n === 2) return 1;
3+
if(n <= 2) return n;
44

5-
let first = 1, second = 1;
5+
let first = 1, second = 2;
66
let temp = 0;
7-
for(let i=0; i<n-1; i++) {
7+
for(let i=2; i<n; i++) {
88
temp = first + second;
99
first = second;
1010
second = temp;
1111
}
1212
return second;
1313
}
1414

15-
// TC: O(n) SC: O(n)
15+
// Bottomup DP -TC: O(n) SC: O(n)
1616
function climbStairs2(n) {
17-
if(n ===1 || n === 2) return 1;
17+
if(n <= 2) return n;
1818

1919
let dp = new Array(n+1);
2020
dp[0] = 1, dp[1] = 1;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
**Description:**
2+
You are required to climb a staircase that consists of `n` individual steps. Determine the total number of distinct ways to reach the top of the staircase.
3+
4+
**Note:** Each time you can either climb 1 or 2 steps.
5+
6+
## Examples:
7+
Example 1:
8+
9+
Input: n = 5
10+
Output: 8
11+
12+
Example 2:
13+
14+
Input: n = 6
15+
Output: 13
16+
17+
**Algorithmic Steps(Approach 1&2)**
18+
This problem is solved efficiently using **dynamic programming** approach by avoiding the recomputations of same subproblems. The algorithmic approach can be summarized as follows:
19+
20+
1. Add a preliminary check like if the step value is less than or equal to 2, return the possible ways to reach them as step's value.
21+
22+
2. Create two variables(`first` and `second`) to store the possible ways to reach the current step and the next step. Since the first step takes one possible way and second step takes two possible ways, those varaibles needs to be initialized to 1 & 2 respectively.
23+
24+
3. Iterate `n-1` times to calculate the next step until `n`th step is reached.
25+
26+
4. The number of possible ways to reach next step is the combination of previous two steps possibilities. Because any particular step is reached either through 1 step or 2 steps.
27+
28+
5. Calculate the sum of first and second pointers in a temporary variable(`temp`). Assign the second pointer variable to first and update the second pointer variable to temporary variable.
29+
30+
6. Repeat steps 4-5 until the nth step is reached.
31+
32+
7. Return second pointer value which indicates the possible ways to reach top stair.
33+
34+
35+
**Time and Space complexity:**
36+
This algorithm has a time complexity of `O(n)`, where `n` is the number of steps. This is because we are traversing all the steps to calculate the possible ways for nth step.
37+
38+
Here, we don't use any additional datastructure other than two variables. Hence, the space complexity will be `O(1)`.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
function dropRightWhile(arr, predicate){
2+
let endIndex = arr.length-1;
3+
4+
while(endIndex >=0 && predicate(arr[endIndex], endIndex, arr)){
5+
endIndex--;
6+
}
7+
8+
return arr.slice(0, endIndex+1);
9+
}
10+
11+
12+
const arr1 = [{
13+
name: 'banana', type: 'fruit'},
14+
{name: 'tomato', type: 'veggie'},
15+
{name: 'apple', type: 'fruit'}
16+
];
17+
console.log(dropRightWhile(arr1, (el) => el.name === 'apple'));

0 commit comments

Comments
 (0)