## Algorithm

Problem Name: 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.

Example 1:

```Input: cost = [10,15,20]
Output: 15
Explanation: You will start at index 1.
- Pay 15 and climb two steps to reach the top.
The total cost is 15.
```

Example 2:

```Input: cost = [1,100,1,1,1,100,1,1,100,1]
Output: 6
Explanation: You will start at index 0.
- Pay 1 and climb two steps to reach index 2.
- Pay 1 and climb two steps to reach index 4.
- Pay 1 and climb two steps to reach index 6.
- Pay 1 and climb one step to reach index 7.
- Pay 1 and climb two steps to reach index 9.
- Pay 1 and climb one step to reach the top.
The total cost is 6.
```

Constraints:

• `2 <= cost.length <= 1000`
• `0 <= cost[i] <= 999`

## Code Examples

### #1 Code Example with C++ Programming

```Code - C++ Programming```

``````& cost) {
int n = cost.size();
vectordp(n);
dp = cost;
dp = cost;
for(int i = 2; i < n; i++)
dp[i] = min(dp[i - 1], dp[i - 2]) + cost[i];
return min(dp[n - 2], dp[n - 1]);
}
};

// If we take a look at the solution above, we can see the result dp[i] only depends on previous 2 steps(dp[i - 1] and dp[i - 2]).
// So we can replace the 'dp' array with 2 variables.
// Here is the final solution:
class Solution {
public:
int minCostClimbingStairs(vector& cost) {
int n = cost.size(), a = cost, b = cost, c;
for(int i = 2; i < n; i++, a = b, b = c)
c = min(a, b) + cost[i];
return min(a, b);
}
};
``````
Copy The Code &

Input

cmd
cost = [10,15,20]

Output

cmd
15

### #2 Code Example with Java Programming

```Code - Java Programming```

``````
class Solution {
public int minCostClimbingStairs(int[] cost) {
int n = cost.length;
int first = cost;
int second = cost;
for (int i = 2; i < n; i++) {
int curr = cost[i] + Math.min(first, second);
first = second;
second = curr;
}
return Math.min(first, second);
}
}
``````
Copy The Code &

Input

cmd
cost = [10,15,20]

Output

cmd
15

### #3 Code Example with Javascript Programming

```Code - Javascript Programming```

``````
const minCostClimbingStairs = function(cost) {
let f1 = cost;
let f2 = cost;
for (let i = 2; i < cost.length; i++) {
let f_cur = cost[i] + Math.min(f1, f2);
f1 = f2;
f2 = f_cur;
}
return Math.min(f1, f2);
};
``````
Copy The Code &

Input

cmd
cost = [1,100,1,1,1,100,1,1,100,1]

Output

cmd
6

### #4 Code Example with Python Programming

```Code - Python Programming```

``````
class Solution:
def minCostClimbingStairs(self, cost):
"""
:type cost: List[int]
:rtype: int
"""
for i in range(2,len(cost)): cost[i]+=min(cost[i-1],cost[i-2])
return min(cost[-1],cost[-2])
``````
Copy The Code &

Input

cmd
cost = [1,100,1,1,1,100,1,1,100,1]

Output

cmd
6

### #5 Code Example with C# Programming

```Code - C# Programming```

``````
using System;

namespace LeetCode
{
public class _0746_MinCostClimbingStairs
{
public int MinCostClimbingStairs(int[] cost)
{
int cost1 = 0, cost2 = 0;
var index = 2;
for (; index < cost.Length; index++)
{
var curr = Math.Min(cost1 + cost[index - 2], cost2 + cost[index - 1]);
cost1 = cost2;
cost2 = curr;
}

return Math.Min(cost1 + cost[index - 2], cost2 + cost[index - 1]);
}
}
}
``````
Copy The Code &

Input

cmd
cost = [10,15,20]

Output

cmd
15