Min Cost Climbing Stairs LeetCode solution in C++

1
class Solution {
2
public:
3
int minCostClimbingStairs(vector<int> &cost) {
4
/* Minimize cost of steps, where you can take one or two steps.
5

6
At each step, store the minimum of the current step plus the step
7
previous or the step two previous. At the last step we can either take
8
the last element or leave it off.
9
*/
10
int n = cost.size();
11
for (int i = 2; i < n; i++) {
12
cost[i] = min(cost[i] + cost[i - 2], cost[i] + cost[i - 1]);
13
}
14

15
return min(cost[n - 1], cost[n - 2]);
16
}
17
};

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0