Leetcode #746. Min Cost Climbing Stairs
题目
https://leetcode.com/problems/min-cost-climbing-stairs/description/
代码
Simple Question. 没啥好说的,数组那里优化下,用两个变量就可以。
class Solution {
public:
int minCostClimbingStairs(vector<int>& cost) {
vector<int> tol(cost.size() + 2);
for (int i = cost.size() - 1; i >= 0; i--)
tol[i] = min(tol[i + 1], tol[i + 2]) + cost[i];
return min(tol[0], tol[1]);
}
};