> For the complete documentation index, see [llms.txt](https://blog.yushunchen.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://blog.yushunchen.com/algo/dynamic-programming/min-cost-climbing-stairs.md).

# Min Cost Climbing Stairs

{% embed url="<https://leetcode.com/problems/min-cost-climbing-stairs>" %}

## Solution 1

```java
class Solution {
    public int minCostClimbingStairs(int[] cost) {
        int[] minCost = new int[cost.length + 1];
        for (int i = 2; i < minCost.length; i++) {
            int oneStep = minCost[i - 1] + cost[i - 1];
            int twoStep = minCost[i - 2] + cost[i - 2];
            minCost[i] = Math.min(oneStep, twoStep);
        } 
        return minCost[minCost.length - 1];
    }
}
```

This is the DP equation:

$$
minCost\[i] = min(minCost\[i-1] + cost\[i-1], minCost\[i-2], cost\[i-2])
$$

For the minimum cost to reach the ith step, we either came from the one-step hop or  from the two-step hop (with their corresponding minimum costs as well).

Time complexity: O(n)

Space complexity: O(n)

## Solution 2

```java
class Solution {
    public int minCostClimbingStairs(int[] cost) {
        int downOne = 0;
        int downTwo = 0;
        for (int i = 2; i < cost.length + 1; i++) {
            int oneStep = downOne + cost[i - 1];
            int twoStep = downTwo + cost[i - 2];
            int tempDownTwo = downOne;
            downOne = Math.min(oneStep, twoStep);
            downTwo = tempDownTwo;
        } 
        return downOne;
    }
}
```

Similarly, we can improve the space complexity by just using variables instead of the whole array. It become a little more abstract now.

`downOne` and `downTwo` represents the minimum cost to reach one step and two steps below the current step that we are taking. Inside the loop, each iteration, `downOne` is after `downTwo`. We calculate the minimum and update `downOne`. Then, `downTwo` is updated to be the old `downOne`.


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://blog.yushunchen.com/algo/dynamic-programming/min-cost-climbing-stairs.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
