Advertisement
i_love_rao_khushboo

Untitled

Sep 16th, 2022
931
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.69 KB | None | 0 0
  1. /*PROBLEM: You are climbing a stair case. It takes n steps to reach to the top. Each
  2.            time you can either climb 1 or 2 steps. In how many distinct ways can you
  3.            climb to the top?
  4.            NOTE: Given n will be a positive integer.
  5. */
  6.  
  7. //RECURSIVE IMPLEMENTATION
  8.  
  9. long long climbingStairs(long long n)
  10. {
  11.     //base condition(s)
  12.     //if no stairs then only 1 way(already at top)
  13.     if(n==0)  
  14.        return 1;
  15.    
  16.     //take one step to reach the end, and this is the only way
  17.     else if(n==1)
  18.        return 1;
  19.    
  20.     //take one step twice or take two steps to reach the end
  21.     else if(n==2)
  22.        return 2;
  23.  
  24.     //choice diagram code
  25.     /*if we take one step, we are left with "n - 1" steps,
  26.       if we take two steps, we are left with "n - 2" steps.
  27.       Now add all the available choices*/
  28.     return climbingStairs(n-1) + climbingStairs(n-2);
  29. }
  30.  
  31. /*Time Complexity: O(2^n) because we are making 2 recursive calls in the same function.
  32.   Space Complexity: O(n) which is used to store the recursion stack.*/
  33.  
  34. *********************************************************************************************************
  35.  
  36. //MEMOIZED IMPLEMENTATION
  37.  
  38. long long climbingStairs(long long n)
  39. {
  40.     //base condition(s)
  41.     //if no stairs then only 1 way(already at top)
  42.     if(n==0)  
  43.        return 1;
  44.    
  45.     //take one step to reach the end, and this is the only way
  46.     else if(n==1)
  47.        return 1;
  48.    
  49.     //take one step twice or take two steps to reach the end
  50.     else if(n==2)
  51.        return 2;
  52.    
  53.     //check if already calculated or not
  54.     else if(dp[n]!=-1)
  55.        return dp[n];
  56.    
  57.     //choice diagram code
  58.     /*if we take one step, we are left with "n - 1" steps,
  59.       if we take two steps, we are left with "n - 2" steps.
  60.       Now add all the available choices*/
  61.     return dp[n]=climbingStairs(n-1) + climbingStairs(n-2);
  62. }
  63.  
  64. //dp[] is a 1 D global matrix/vector, with size (n+1) and initialized
  65. //with -1, memset(dp, -1, sizeof(dp));
  66.  
  67. /*Time Complexity: O(n) because memoization array dp[n+1] stores the results of all
  68.   sub-problems. We can conclude that we will not have more than n + 1 sub-problems.
  69.   Space Complexity: O(n) which is used to store the dp[] array.
  70. */
  71.  
  72. *********************************************************************************************************
  73.  
  74. //TABULATION IMPLEMENTATION (Real DP ;))
  75.  
  76. long long climbingStairs(long long n)
  77. {
  78.     //initialisation of dp vector
  79.     dp[0]=1; dp[1]=1; dp[2]=2;
  80.  
  81.     //choice diagram code iterative version
  82.     for(long long i=3; i<(n+1); i++)
  83.        dp[i]=dp[i-1]+dp[i-2];
  84.  
  85.     return dp[n];
  86. }
  87.  
  88. //dp[] is a 1 D global matrix/vector, with size (n+1)
  89. /*Time Complexity: O(n)
  90.   Space Complexity: O(n) which is used to store the dp[] array.
  91. */
  92.  
  93. *********************************************************************************************************
  94.  
  95. //MEMORY OPTIMIZED IMPLEMENTATION
  96. /*As we can see from the visualization of the recursive stack and other solutions,
  97.   to be able to calculate the n, you need the value of last two combinations: n-1
  98.   and n-2.
  99.   These two values are enough and we don’t need to store all other past combinations.
  100. */
  101.  
  102. long long climbingStairs(long long n)
  103. {
  104.     //base condition(s)
  105.     //if no stairs then only 1 way(already at top)
  106.     if(n==0)  
  107.        return 1;
  108.    
  109.     //take one step to reach the end, and this is the only way
  110.     else if(n==1)
  111.        return 1;
  112.    
  113.     //take one step twice or take two steps to reach the end
  114.     else if(n==2)
  115.        return 2;
  116.    
  117.     long long first=1, second=2, third;
  118.     for(long long i=3; i<(n+1); i++)
  119.     {
  120.        third=second+first;
  121.        first=second;
  122.        second=third;
  123.     }
  124.  
  125.     return third;
  126. }
  127.  
  128. //Time Complexity: O(n)
  129. //Space Complexity: O(1)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement