-
Notifications
You must be signed in to change notification settings - Fork 0
/
70.climbing-stairs.cpp
68 lines (61 loc) · 1.1 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
/*
* @lc app=leetcode id=70 lang=cpp
*
* [70] Climbing Stairs
*/
// @lc code=start
// https://leetcode.com/articles/climbing-stairs/
// dp based solution
class Solution
{
public:
int climbStairs(int n)
{
vector<int> dp(n + 1, 0);
dp[0] = 1;
dp[1] = 1;
for (int i = 2; i <= n; ++i)
{
dp[i] = dp[i - 1] + dp[i - 2];
}
return dp[n];
}
};
/*
// recursive approach
class Solution {
public:
int climbStairs(int n) {
int count = 0;
help(n,count);
return count;
}
void help(int n, int &count) {
if(n<0) {
return;
}
if(n == 0) {
count++;
return;
}
help(n-1,count);
help(n-2,count);
}
};
*/
/*
// using constant space
class Solution {
public:
int climbStairs(int n) {
vector<int> steps(3, 0);
steps[0] = 1;
steps[1] = 1;
for (int i = 2; i <= n; ++i) {
steps[i % 3] = steps[(i - 1) % 3] + steps[(i - 2) % 3];
}
return steps[n % 3];
}
};
*/
// @lc code=end