-
Notifications
You must be signed in to change notification settings - Fork 0
/
70. Climbing Stairs.cpp
73 lines (54 loc) · 1.66 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
70
71
72
73
// -----Approach 1: Space Optimisation ------------------------------------------------------------
/*
Problem Link: https://leetcode.com/problems/climbing-stairs/
Time: 0 ms (Beats 100%), Space: 6 MB (Beats 42.22%)
*/
class Solution {
public:
int climbStairs(int n) {
int prev2=0, prev=1, curr;
if(n<=1) return 1;
for(int i=1; i<=n; i++){ // we can also run from 2 -> n+1 according to the problem
curr= prev + prev2;
prev2= prev;
prev= curr;
}
return curr;
}
};
// -----Approach 2: Memoization ------------------------------------------------------------
/*
Problem Link: https://leetcode.com/problems/climbing-stairs/
Time: 0 ms (Beats 100%), Space: 6.2 MB (Beats 31.2%)
*/
class Solution {
private:
int count(int idx, vector<int>& dp){
if(idx == 0 || idx == 1) return 1;
if(dp[idx] != -1) return dp[idx];
return dp[idx]= count(idx-1, dp) + count(idx-2, dp);
}
public:
int climbStairs(int n) {
if(n==0 || n==1) return 1;
vector<int> dp(n+1, -1);
return count(n, dp);
}
};
// -----Approach 2: Tabulation ------------------------------------------------------------
/*
Problem Link: https://leetcode.com/problems/climbing-stairs/
Time: 0 ms (Beats 100%), Space: 6.3 MB (Beats 17.24%)
*/
class Solution {
public:
int climbStairs(int n) {
vector<int> dp(n+1, -1);
dp[0]=1, dp[1]=1;
if(n==0 || n==1) return dp[n];
for(int i=2; i<=n; i++){
dp[i]= dp[i-1] + dp[i-2];
}
return dp[n];
}
};