-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQ2_climibingStairs.cpp
More file actions
45 lines (39 loc) · 865 Bytes
/
Q2_climibingStairs.cpp
File metadata and controls
45 lines (39 loc) · 865 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#include<iostream>
#include<vector>
using namespace std;
int recursiveSol(int n){
//base case
if(n == 1 || n ==0){
return 1;
}
if(n==2){
return 2;
}
int ans = recursiveSol(n-1) + recursiveSol(n-2);
return ans;
}
//topb to bobttom approch
int topToBottom(int n, vector<int>&dp){
//base case
if(n < 2){
return 1;
}
if(n ==2){
return 2;
}
// step 3: check the solution is already exist or not
if(dp[n] != -1){
return dp[n];
}
//step 2 : store the ans into dp Array
dp[n] = topToBottom(n-1, dp) + topToBottom(n-2 , dp);
return dp[n];
}
int main(){
int n =40;
//creating the dp array
vector<int> dp( n+1 , -1);
int ans = topToBottom(n, dp);
cout<<"The Ans is: "<<ans<<endl;
return 0;
}