-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy path70.cpp
More file actions
31 lines (30 loc) · 680 Bytes
/
70.cpp
File metadata and controls
31 lines (30 loc) · 680 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
class Solution {
public:
int climbStairs(int n) {
if (n == 1) return 1;
if (n == 2) return 2;
vector<int> dp(n + 1, 0);
dp[0] = 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];
}
};
class Solution {
public:
int climbStairs(int n) {
if (n == 1) return 1;
if (n == 2) return 2;
int prevTwo = 1;
int prevOne = 2;
for (int i = 2; i < n; ++i) {
int current = prevTwo + prevOne;
prevTwo = prevOne;
prevOne = current;
}
return prevOne;
}
};