forked from liuyubobobo/Play-Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main2.cpp
38 lines (28 loc) · 839 Bytes
/
main2.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
/// Source : https://leetcode.com/problems/min-cost-climbing-stairs/
/// Author : liuyubobobo
/// Time : 2017-12-16
#include <iostream>
#include <vector>
using namespace std;
/// Dynamic Programming
/// Time Complexity: O(n)
/// Space Complexity: O(n)
class Solution {
public:
int minCostClimbingStairs(vector<int>& cost) {
cost.push_back(0);
vector<int> dp(cost.size(), 0);
dp[0] = cost[0];
dp[1] = cost[1];
for(int i = 2 ; i < cost.size() ; i ++)
dp[i] = min(dp[i-1], dp[i-2]) + cost[i];
return dp.back();
}
};
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;
}