Count ways to reach the Nth stair | Set-2
Last Updated :
26 Apr, 2022
There are N stairs, a person standing at the bottom wants to reach the top. The person can climb either 1 stair or 2 stairs at a time. Count the number of ways, the person can reach the top.
Nth-stairs
Examples:
Input: N = 1
Output: 1
Explanation: There is only one way to climb 1st stair
Input: N= 2
Output: 2
Explanation: There are two ways and the sequence to climb to Nth stair are: (1, 2), (2)
Input: N = 4
Output: 5
Explanation: There are five possible ways and the sequence to climb to Nth stair are:
(1, 2, 3, 4), (1, 2, 4), (2, 3, 4), (1, 3, 4), (2, 4)
The dynamic programming and sliding window and several other approaches are discussed in Set-1 of this problem. But this approach will have a better overall auxiliary space.
Approach: This problem can be solved with better space complexity based on the following observation:
If observed closely, the value of current state ( i ) depends on two states: the value of the previous state (i – 1) and the value of (i – 2) state.
Instead of using extra space only two variables can be used to store the values of the above-mentioned two states.
As one can either climb one step or two step at a time, so ith state value is the summation of the values of (i – 1)th and (i – 2)th state.
Follow the illustration given below for a better understanding.
Illustration:
Initially, There are one ways to reach 1st stair and the sequence to climb to 1st stair is: (1)
There are two ways to reach 2nd stair and the sequence to climb to 2nd stair are: (1, 2), (2)
For 3rd stair:
=> Number of ways to reach 3rd stair = prev1 + prev2. i.e. curr = 2 + 1 = 3.
=> prev2 = prev1 = 2. prev1 = curr = 3
For 4th stair:
=> Number of ways to reach 3rd stair = prev1 + prev2. i.e. curr = 3 + 2 = 5.
=> prev2 = prev1 = 3. prev1 = curr = 5
For 5th stair:
=> Number of ways to reach 3rd stair = prev1 + prev2. i.e. curr = 5 + 3 = 8.
=> prev2 = prev1 = 5. prev1 = curr = 8
For 6th stair:
=> Number of ways to reach 3rd stair = prev1 + prev2. i.e. curr = 8 + 5 = 13.
=> prev2 = prev1 = 8. prev1 = curr = 13
Below is the image representation of each step of above explanation:
Follow the steps mentioned below to solve the problem:
- Declare two variables (say, prev1 and prev2) to store the previous two states for any current state and initialize them with the number of ways to reach the 1st and 2nd stair.
- Iterate from 3rd stair to Nth stair:
- Calculate the number of ways to reach the current stair as shown in the above observation.
- Now update the prev1 and prev2 variables as per the observation.
- Return the value for the Nth stair obtained as the result.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int minSteps( int N)
{
if (N == 1)
return 1;
if (N == 2)
return 2;
int curr;
int prev2 = 1;
int prev1 = 2;
for ( int i = 3; i <= N; i++) {
curr = prev1 + prev2;
prev2 = prev1;
prev1 = curr;
}
return curr;
}
int main()
{
int N = 6;
cout << minSteps(N);
return 0;
}
|
Java
import java.util.*;
class GFG {
static int minSteps( int N)
{
if (N == 1 )
return 1 ;
if (N == 2 )
return 2 ;
int curr = 0 ;
int prev2 = 1 ;
int prev1 = 2 ;
for ( int i = 3 ; i <= N; i++) {
curr = prev1 + prev2;
prev2 = prev1;
prev1 = curr;
}
return curr;
}
public static void main (String[] args) {
int N = 6 ;
System.out.print(minSteps(N));
}
}
|
Python3
def minSteps(N):
if N = = 1 :
return 1
if N = = 2 :
return 2
prev1, prev2 = 2 , 1
for i in range ( 3 , N + 1 ):
curr = prev1 + prev2
prev2 = prev1
prev1 = curr
return curr
N = 6
print (minSteps(N))
|
C#
using System;
class GFG {
static int minSteps( int N)
{
if (N == 1)
return 1;
if (N == 2)
return 2;
int curr = 0;
int prev2 = 1;
int prev1 = 2;
for ( int i = 3; i <= N; i++) {
curr = prev1 + prev2;
prev2 = prev1;
prev1 = curr;
}
return curr;
}
public static void Main()
{
int N = 6;
Console.Write(minSteps(N));
}
}
|
Javascript
<script>
const minSteps = (N) => {
if (N == 1)
return 1;
if (N == 2)
return 2;
let curr;
let prev2 = 1;
let prev1 = 2;
for (let i = 3; i <= N; i++) {
curr = prev1 + prev2;
prev2 = prev1;
prev1 = curr;
}
return curr;
}
let N = 6;
document.write(minSteps(N));
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...