> ## Documentation Index
> Fetch the complete documentation index at: https://leetcode-py.wisl.dev/llms.txt
> Use this file to discover all available pages before exploring further.

> ## Agent Instructions
> leetcode-py is a Python LeetCode practice environment generator with one CLI: lcpy. It is not a service or platform.
> Each problem is a directory under leetcode/ with README.md, solution.py, test_solution.py, helpers.py, and playground.ipynb. lcpy gen creates them from JSON templates bundled with the package.
> Examples are backed by tests; copy them verbatim.

# Min Cost Climbing Stairs Python Solution

> Tested Python solution for LeetCode 746 with 13 pytest cases. Generate a practice environment with lcpy.

LeetCode 746, Easy. Topics: Array, Dynamic Programming. [View on LeetCode](https://leetcode.com/problems/min-cost-climbing-stairs/description/).

Generate this problem as a practice environment: tested reference solution, 13 [parametrized pytest cases](/practice/testing), and a playground notebook:

```bash theme={"theme":{"light":"github-light","dark":"github-dark"}}
lcpy gen -n 746   # by problem number
lcpy gen -s min_cost_climbing_stairs   # by problem name
```

## Problem

You are given an integer array `cost` where `cost[i]` is the cost of `i<sup>th</sup>` 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*.

### Examples

```
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.
```

```
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

## Solution

Reference implementation from [solution.py on GitHub](https://github.com/wislertt/leetcode-py/blob/main/leetcode/min_cost_climbing_stairs/solution.py), full suite in [test\_solution.py](https://github.com/wislertt/leetcode-py/blob/main/leetcode/min_cost_climbing_stairs/test_solution.py):

```python theme={"theme":{"light":"github-light","dark":"github-dark"}}
class Solution:
    # Time: O(n)
    # Space: O(1)
    def min_cost_climbing_stairs(self, cost: list[int]) -> int:
        # dp[i] = min cost to reach step i (top is index n).
        # Start at step 0 or 1 for free, so dp[0] = dp[1] = 0.
        prev_two, prev_one = 0, 0  # dp[i-2], dp[i-1]
        for step_cost in cost:
            current = step_cost + min(prev_one, prev_two)
            prev_two, prev_one = prev_one, current
        # Top reached from either of the last two steps (already paid)
        return min(prev_one, prev_two)
```

## Complexity

| Time | Space |
| ---- | ----- |
| O(n) | O(1)  |

## Tags

[NeetCode 150](/catalog/neetcode-150), [NeetCode 250](/catalog/neetcode-250), [NeetCode All](/catalog/neetcode).
