Count ways to N'th Stair(Order does not matter) | Practice | GeeksforGeeks

There are N stairs, and 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 not matter).
Note:
Order does not matter means for n = 4 {1 2 1},{2 1 1},{1 1 2} are considered same.
 
Example 1:
Input: N = 4
Output: 3
Explanation: Three ways to reach at 4th stair.
They are {1, 1, 1, 1}, {1, 1, 2}, {2, 2}.

Example 2:
Input: N = 5
Output: 3
Explanation: Three ways to reach at 5th stair.
They are {1, 1, 1, 1, 1}, {1, 1, 2, 1} and
{1, 2, 2}.


Your Task:
You don't need to read or print anyhting. Your task is to complete the function nthStair() which takes N as input parameter and returns the total number of ways to reach at Nth stair.
 
Expected Time Complexity: O(N)
Expected Space Complexity: O(N)
 
Constraints:
1 ≤ N ≤ 104
+1
syedmohdgulambaquer1 week ago
int dp[10001][3];
	    int solve(int arr[], int n, int i) {
	        
	        if(n == 0) {
	            return 1;
	        }
	        
	        if(i == 0) {
	            return 0;
	        }
	        
	        if(dp[n][i] != -1) {
	            return dp[n][i];
	        }
	        
	        if(n >= arr[i-1]) {
	            return dp[n][i] = solve(arr, n - arr[i-1] , i) + solve(arr, n , i-1);
	        } 
	        
	        return dp[n][i] = solve(arr, n , i-1);
	        
	        
	    }
		int nthStair(int n) {
		    
		   memset(dp, -1, sizeof(dp));
		   int arr[2] = {1,2};
		   
		   return solve(arr, n, 2);
		}
0
jintan2 weeks ago
int nthStair(int n){
     vector<int> v(n+1, 1);
     for(int i = 2; i<n+1; i++){
         v[i] += v[i-2];
     }
     return v[n];
 }
 
+2
user_692 months ago
DP ki jrurat h kya h isme :)
		int nthStair(int n){
		    if(n==0)
		        return 1;
		    if(n%2==0)
		        return 1+nthStair(n-1);
		    else
		        return nthStair(n-1);
		}
+2
kashyapjhon3 months ago
C++ Solution Time=(0.01/1.12) EASY:
int nthStair(int n){     //  Code here     vector<int> dp(n+1,0);     dp[1]=1;     dp[2]=2;     for(int i=3;i<=n;i++){         dp[i]=dp[i-2]+1;     }     return dp[n]; }
+1
abhishekvicky123454 months ago
/*Java 1 line code*/
class Solution{   public long nthStair(int n)   {       return n/2+1;   }}
0
himanshukug19cs5 months ago
java sol
 
 long[] dp = new long[n+1];       dp[0]=0;       dp[1]=1;       if(n>1){           dp[2]=2;       for(int i=3;i<=n;i++){           dp[i]=dp[i-2]+1;       }}       return dp[n];         }
+4
utkarsh51k5 months ago
Fir ganja phook k difficulty set kiya gya hai
0
goyalgarvit809
This comment was deleted.
+1
2001shivank7 months ago
hey, Kindly understand that logic of “return (n/2)+1” is correct. But Why???..............see there is 1 way of climbing 1 step one by 1 , while n/2 is for when the person climbs 2 steps once and rest all as 1, 2 steps twice and then rest all as 1. Example for understanding n/2-----→ n=5 --→ {2,1,1}, {2,2,1}...........................n=6-→ {2,2,2},{2,2,1,1},{2,1,1,1,1}  
+1
yashkatiyar6307 months ago
simple solution
int nthStair(int n){
		    return (n/2)+1;
		}
using recursion+memorization
class Solution{
	public:
	int dp[10001][3];
    Solution(){memset(dp,-1,sizeof(dp));} 
    int knapSack(int n,int val[],int i)
    {
        if(n==0)return 1;
        if(n<0||i<1)return 0;
        if(dp[n][i]!=-1)return dp[n][i];
        else return dp[n][i]=knapSack(n-val[i-1],val,i)+knapSack(n,val,i-1);
    }
	int nthStair(int n){
	    int arr[2]={1,2};
	   return knapSack(n,arr,2);
	}
};
 
 
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.
Make sure you are not using ad-blockers.
Disable browser extensions.
We recommend using latest version of your browser for best experience.
Avoid using static/global variables in coding problems as your code is tested 
                                against multiple test cases and these tend to retain their previous values.
Passing the Sample/Custom Test cases in coding problems 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.