# Min Cost Climbing Stairs Problem & Solution

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.

## C++ Solution

The runtime complexity of this algorithm is $O(n)$.

#pragma GCC optimize("Ofast")
#pragma GCC optimization("unroll-loops")

static const int _=[](){ios::sync_with_stdio(false);cin.tie(nullptr);cout.tie(nullptr);return 0;}();

class Solution {
public:
int minCostClimbingStairs(vector<int>& cost) {

// Note that the length of the total vector is larger by one compared to the cost vector.
vector<int> total(cost.size() + 1, 0);
total[0] = cost[0];
total[1] = cost[1];

for (int i = 2; i < total.size(); ++i) {

// Takes the minimum total from the i - 1th and i - 2th step, and adds up the current cost.
total[i] = min(total[i - 1], total[i - 2]) +
(i < cost.size() ? cost[i] : 0);
}