> For the complete documentation index, see [llms.txt](https://grind75-notes.gitbook.io/notes/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://grind75-notes.gitbook.io/notes/week-2/climbing-stairs.md).

# Climbing Stairs

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

### Problem

> You are climbing a staircase. It takes `n` steps to reach the top.
>
> Each time you can either climb `1` or `2` steps. In how many distinct ways can you climb to the top?
>
> &#x20;
>
> **Example 1:**
>
> <pre><code>Input: n = 2
> <strong>Output: 2
> </strong><strong>Explanation:
> </strong>There are two ways to climb to the top.
> 1. 1 step + 1 step
> 2. 2 steps
> </code></pre>
>
> **Example 2:**
>
> <pre><code>Input: n = 3
> <strong>Output: 3
> </strong><strong>Explanation:
> </strong>There are three ways to climb to the top.
> 1. 1 step + 1 step + 1 step
> 2. 1 step + 2 steps
> 3. 2 steps + 1 step
>
> </code></pre>

### Pseudocode

{% code overflow="wrap" %}

```
establish some relationship with each increase in step, find a general pattern

n = 4
1. 1 step + 1 step + 1 step + 1 step     ...(3 steps)
2. 1 step + 2 steps + 1 step             ...(3 steps)
3. 2 steps + 1 step + 1 step             ...(3 steps)
4. 1 step + 1 step + 2 steps             ...(2 steps)
5. 2 steps + 2 steps                     ...(2 steps)

to get to 4 steps 
    - increase the number of ways to get to 3 steps + 1 step to 4 steps
    - increase the number of ways to get to 2 steps + 2 steps to 4 steps

ways(n) = ways(n - 1) + ways(n - 2)

for other dp solutions see:
https://leetcode.com/problems/climbing-stairs/solutions/1792723/python-in-depth-walkthrough-explanation-dp-top-down-bottom-up/?orderBy=most_votes

```

{% endcode %}

### Solution

```javascript
var climbStairs = function (n) {
  if (n <= 3) {
    return n;
  }

  let a = [1, 2];

  for (let i = 3; i <= n; i++) {
    console.log(a);
    a = [a[1], a[0] + a[1]];
  }

  return a[1];
};

```

### Time and Space Complexity

#### Time

* loops over from 3 to n - O(N), summation assignment O(1)
* Total - O(N)

#### Space

* assignment to 2 element array in every loop - O(1) always two elements, doesn't expand with input
* Total - O(1)


---

# 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:

```
GET https://grind75-notes.gitbook.io/notes/week-2/climbing-stairs.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
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.
