746. Min Cost Climbing Stairs
Input: cost = [10, 15, 20]
Output: 15
Explanation: Cheapest is start on cost[1], pay that cost and go to the top.Input: cost = [1, 100, 1, 1, 1, 100, 1, 1, 100, 1]
Output: 6
Explanation: Cheapest is start on cost[0], and only step on 1s, skipping cost[3].// Dynamic Programming
int minCostClimbingStairs(vector<int>& cost) { // time: O(n); space: O(n)
int n = cost.size();
if (n <= 2) return 0;
vector<int> c(n + 1, 0);
for (int i = 2; i <= n; ++i) {
c[i] = min(c[i - 2] + cost[i - 2], c[i - 1] + cost[i - 1]);
}
return c.back();
}Last updated