-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
32 lines (30 loc) · 810 Bytes
/
main.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
class Solution {
public:
int minCostClimbingStairs(vector<int>& cost) {
array<int, 2> dp = {0, 0};
for (int i = 2; i <= cost.size(); ++i) {
dp = {dp[1], min(dp[0] + cost[i - 2], dp[1] + cost[i - 1])};
}
return dp.back();
}
};
class Solution {
public:
int minCostClimbingStairs(vector<int>& cost) {
array<int, 2> dp = {cost[0], cost[1]};
for (int i = 2; i < cost.size(); ++i) {
dp = {dp[1], min(dp[0], dp[1]) + cost[i]};
}
return min(dp[0], dp[1]);
}
};
class Solution {
public:
int minCostClimbingStairs(vector<int>& cost) {
array<int, 2> dp = {0, 0};
for (int v : cost) {
dp = {dp[1], min(dp[0], dp[1]) + v};
}
return min(dp[0], dp[1]);
}
};