forked from liuyubobobo/Play-Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main5.cpp
40 lines (30 loc) · 822 Bytes
/
main5.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
/// Source : https://leetcode.com/problems/min-cost-climbing-stairs/
/// Author : liuyubobobo
/// Time : 2018-10-29
#include <iostream>
#include <vector>
using namespace std;
/// Dynamic Programming
/// Reverse order to dp :-)
///
/// Time Complexity: O(n)
/// Space Complexity: O(1)
class Solution {
public:
int minCostClimbingStairs(vector<int>& cost) {
int a = 0, b = 0;
for(int i = cost.size() - 1 ; i >= 0 ; i --){
int c = min(a, b) + cost[i];
a = b;
b = c;
}
return min(a, b);
}
};
int main() {
vector<int> vec1 = {10, 15, 20};
cout << Solution().minCostClimbingStairs(vec1) << endl;
vector<int> vec2 = {1, 100, 1, 1, 1, 100, 1, 1, 100, 1};
cout << Solution().minCostClimbingStairs(vec2) << endl;
return 0;
}