[LeetCode] Min Cost Climbing Stairs

746. Min Cost Climbing Stairs

You are given an integer array cost where cost[i] is the cost of ith step on a staircase. Once you pay the cost, you can either climb one or two steps.

You can either start from the step with index 0, or the step with index 1.

Return the minimum cost to reach the top of the floor.

1
2
3
4
5
6
7
8
9
10
class Solution {
public:
int minCostClimbingStairs(vector<int>& cost) {
int size = cost.size();
for(int i = 2; i < size; i++) {
cost[i] = cost[i] + min(cost[i-1], cost[i-2]);
}
return min(cost[size-1], cost[size-2]);
}
};
Author: Song Hayoung
Link: https://songhayoung.github.io/2022/02/02/PS/LeetCode/min-cost-climbing-stairs/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.