-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path70. Climbing Stairs.cpp
69 lines (52 loc) · 1.64 KB
/
70. Climbing Stairs.cpp
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
/************* Method - 1 (Top-Down Approach; TC - O(N); SC - O(N)) **********
class Solution {
public:
// Returns the number of ways you can climb to top
int climbStairsHelper(int n, vector<int> &dp) {
// Base case
if(n == 1)
return 1;
if(n == 2)
return 2;
// Recursive step
// Have I solved the subproblem?
if(dp[n] != -1)
return dp[n];
// If I haven't solved it.
// Solve it now and store it
dp[n] = climbStairsHelper(n-1, dp) + climbStairsHelper(n-2, dp);
return dp[n];
}
int climbStairs(int n) {
vector<int> dp(n+1, -1);
return climbStairsHelper(n, dp);
}
};
********************************************************************/
/************* Method - 2 (Bottom-Up Approach; TC - O(N); SC - O(N)) **********
class Solution {
public:
int climbStairs(int n) {
if(n == 1) return 1;
vector<int> dp(n+1);
dp[1] = 1;
dp[2] = 2;
for(int i = 3; i <= n; i++)
dp[i] = dp[i-1] + dp[i-2];
return dp[n];
}
};
*******************************************************************************/
/************* Method - 3 (Bottom-Up Approach; TC - O(N); SC - O(1)) **********/
class Solution {
public:
int climbStairs(int n) {
if(n == 1) return 1;
vector<int> dp(2);
dp[1%2] = 1;
dp[2%2] = 2;
for(int i = 3; i <= n; i++)
dp[i%2] = dp[(i-1)%2] + dp[(i-2)%2];
return dp[n%2];
}
};