Count ways to reach the n'th stair | Practice | GeeksforGeeks

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 (order does matter).
Example 1:
Input:
n = 4
Output: 5
Explanation:
You can reach 4th stair in 5 ways. 
Way 1: Climb 2 stairs at a time. 
Way 2: Climb 1 stair at a time.
Way 3: Climb 2 stairs, then 1 stair
and then 1 stair.
Way 4: Climb 1 stair, then 2 stairs
then 1 stair.
Way 5: Climb 1 stair, then 1 stair and
then 2 stairs.

Example 2:
Input:
n = 10
Output: 89 
Explanation: 
There are 89 ways to reach the 10th stair.

Your Task:
Complete the function countWays() which takes the top stair number m as input parameters and returns the answer % 10^9+7.
Expected Time Complexity : O(n)
Expected Auxiliary Space: O(1)
Constraints:
1 ≤ n ≤ 104
0
fpouuc6g15gpbb5he4q9k3uvb20hihh1bxzvc7jn3 weeks ago
C++ Solution:
int countWays(int n){
        int prev1 = 1,prev2 = 1,MOD = 1e9 + 7;
        for(int i = 2; i<=n; i++){
            long long curr = 1LL*prev1 + prev2;
            prev2 = prev1;
            prev1 = curr%MOD;
        }
        return prev1;
    }
+1
danishsharma4963 weeks ago
NO BAKWAS SIMPLE CPP SOLN USING RECURSION AND MEMOIZATION
 
  long long int  memo[100000];   int pow = 1000000007;      long long int countWays(int n)   {       if(n ==0 ) return 1 ;       if(n < 0 ) return 0 ;              if(memo[n]!= 0 ) return memo[n];             memo[n] = (countWays(n-1) + countWays(n-2) ) %pow;             return memo[n];          }
0
devashishbakare1 month ago
JAVA : Using DP(Tabulation)
 public static int solve(int n, int tab[])
    {
        tab[0] = 1;
        int pow = 1000000007;
        
        for(int i = 1 ; i <= n ; i++)
        {
            if(i == 1)
            tab[i] = tab[i-1];
            else{
                tab[i] = (tab[i-1] + tab[i-2])%pow;
            }
        }
        return tab[n];
        
    }
0
sudhanshujha9611 month ago
Dp + Space Optimisation
 int countWays(int n)
    {
        // your code here
        int prev = 1 , prev2 = 1;
        for(int i = 2; i<=n; i++){
           int curr = (prev + prev2)%1000000007;
           prev2 = prev;
           prev = curr;
        }
        return prev;
    }
0
dronzerdracel1 month ago
Recursion+Memoization
int fib(int n,int *dp){
        if(n<0)
         return dp[n]=0;
        if(n==0)
          return dp[n]=1;
        if(dp[n]!=-1)
         return dp[n];
        dp[n]=(fib(n-1,dp)+fib(n-2,dp))%1000000007;
        return dp[n];
    }
    int countWays(int n)
    {
        // your code here
        int dp[n+1];
        memset(dp,-1,sizeof(dp));
        fib(n,dp);
        return dp[n];
    }
+1
ashayvsant1 month ago
Simple Recursive solution in Java -
class Solution
{
    static int dp[];
    //Function to count number of ways to reach the nth stair.
    int countWays(int n)
    {
        // your code here
        dp = new int[n];
        for(int i=0; i<n; i++) dp[i] = -1;
        return solve(n, 1);
    }
    
    int solve(int n, int currentStair) {
        if(currentStair >= n) return 1;
        
        if(dp[currentStair] != -1)
            return dp[currentStair];
        
        dp[currentStair] = (solve(n, currentStair+1) + solve(n, currentStair+2))%(1000000007);
        return dp[currentStair];
    }
}
0
harshsinghreal2 months ago
VERY SIMPLE C++ SOLUTION-→
Hope you like it (kindly up vote it  😁).
int countWays(int n)
    {
        
        int a = 1, b = 1,j;
    for (int i = 1;i < n;i++) {
         j=b;
         b=(b+a)%1000000007;
         a = j;
    }
    return b;
    }
+2
sdmrf2 months ago
Python Soln:
    def countWays(self,n,memo={}):
       mod=10**9+7
       if n in memo:
           return memo[n]%mod
       elif n==0 or n==1:
           return 1
       elif n==2:
           return 2
       else:
           memo[n]= self.countWays(n-1,memo)%mod + self.countWays(n-2,memo)%mod
           return memo[n]%mod
+1
kashyapjhon2 months ago
C++ Solution Using DP Time=(0.0/1.1):
long long int mod= 1000000007;   int help(vector<int> &dp,int sum,int n){       if(sum==0 || sum==1){           return 1;       }       if(dp[sum]!=-1){           return dp[sum];       }       if(sum>=2){           return dp[sum]=(help(dp,sum-2,n)+help(dp,sum-1,n))%mod;       }          }      int countWays(int n)   {       // your code here       int count=0;       vector<int> dp(n+1,-1);       int sum=n;       return help(dp,sum,n);   }
0
kuldeepy104592 months ago
 int mod =1e9+7;   int countWays(int n)   {       // your code here       vector<int>output(n+1);       output[0]=1;       output[1]=1;       for(int i=2;i<=n;i++){           output[i]=(output[i-1]+output[i-2])%mod;       }       return output[n]%mod;   }
We strongly recommend solving this problem on your own before viewing its editorial. Do you still
                    want to view the editorial?
 Login to access your submissions. 

Problem


Contest

Reset the IDE using the second button on the top right corner.
Avoid using static/global variables in your code as your code is tested against multiple test cases and these tend to retain their previous values.
Passing the Sample/Custom Test cases does not guarantee the correctness of code. On submission, your code is tested against multiple test cases consisting of all possible corner cases and stress constraints.
You can access the hints to get an idea about what is expected of you as well as the final solution code.
You can view the solutions submitted by other users from the submission tab.