-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path746.使用最小花费爬楼梯.cpp
48 lines (47 loc) · 1.05 KB
/
746.使用最小花费爬楼梯.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
#include <vector>
using namespace std;
/*
* @lc app=leetcode.cn id=746 lang=cpp
*
* [746] 使用最小花费爬楼梯
*/
/**
* using dp
* dp[i] 为到达第 i 阶的最小花费
* 楼层顶部为 最后一阶楼梯之后
* dp方程: dp[i] = min(dp[i - 1] + cost[i - 1], dp[i - 2] + cost[i - 2])
* 时间复杂度: O(n)
* 空间复杂度: O(n)
*/
class Solution_DP {
public:
int minCostClimbingStairs(vector<int>& cost) {
int size = cost.size();
vector<int> dp(size + 1, 0);
dp[0] = 0;
dp[1] = 0;
for (int i = 2; i <= size; i++) {
dp[i] = min(dp[i - 1] + cost[i - 1], dp[i - 2] + cost[i - 2]);
}
return dp[size];
}
};
// @lc code=start
/**
* dp 优化, 只保留需要的两个状态
* 时间复杂度: O(n)
* 空间复杂度: O(1)
*/
class Solution {
public:
int minCostClimbingStairs(vector<int>& cost) {
int pre = 0, cur = 0;
for (int i = 2; i <= cost.size(); i++) {
int temp = cur;
cur = min(cur + cost[i - 1], pre + cost[i - 2]);
pre = temp;
}
return cur;
}
};
// @lc code=end