链接:746. 使用最小花费爬楼梯
题解:https://leetcode-cn.com/problems/min-cost-climbing-stairs/solution/shi-yong-zui-xiao-hua-fei-pa-lou-ti-by-l-ncf8/
class Solution {
public:
int minCostClimbingStairs(vector<int>& cost) {
if(cost.size() <= 0) {
return 0;
}
// dp[i],表示到达第i个下标的位置,需要的cost
// dp[i] = min(dp[i-1]+cost[i], dp[i-2]+cost[i])
vector<int> dp(cost);
for(int i = 2; i < dp.size(); ++i) {
dp[i] = min(dp[i-1]+cost[i], dp[i-2]+cost[i]);
}
// 到达重点有两种情况
//1.从dp[cost.size()-1],走1步
//2.或者从dp[cost.size()-2]走两步
return min(dp[cost.size()-1], dp[cost.size()-2]);
}
};