You’re 70. climbing stairs.It will take `n`

steps to get to the top.

Each time you’re able to take `1`

or `2`

steps. In what ways can you get up to your destination?

**Example 1:**

Input:n = 2Output:2Explanation:There are two ways to climb to the top. 1. 1 step + 1 step 2. 2 steps

**Example 2:**

Input:n = 3Output:3Explanation: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

# Analysis:

The question asks you to identify all possible combinations that can bring you to the top and with the limitation that you can go up a couple of steps each time. Thus, at every step, you can choose to step over it or not do it, which is the principle behind dynamic programming.

Let’s look at some examples and see if we can locate a pattern.

**1 Step**

If you are given one step, you can only climb it in one direction, from the ground to the first floor.

**2 Steps**

If there are 2 steps to go, you could climb to the top via 0-1-2 or 0-2 directly, resulting in two routes.

Start at 0, and you have two choices: start with the first step or skip your first step. If you choose one step at a time, you will get to step 1, and you need to go to the next step that will bring you up the ladder; step 2 is the best option in this instance. This is the first option.

Returning to the situation if you decided not to do step 1. In this scenario, you can jump straight to step 2. This will provide you with a second option.

**3 Steps**

Let’s look at what happens in a situation where you are faced with three steps. The first step is zero, and you have two choices, whether you go through step 1 or choose not to. If you go with this first step, you must select the same option as step 2. Suppose you choose to take step 2 or not. If you decide to do step 2, the next step is step 3; it’s a different way to go.

If you do not want to proceed to step 2, you can immediately skip step 3 since you can do two degrees simultaneously. Your second route.

The third route is when you decide to skip step one; you miss step 2 before moving to step 3.

**4 Steps**

Let’s take a look at another illustration. Based on the same analysis conducted for examples 3-4, we can determine you could reach the top of step 4 in five different ways.

**The pattern we see in this case will be that the sum of n = (n-1) (n-1) and (n 2)). It’s a Fibonacci sequence!**

# Solutions 1 and 2: Recursion and Memorization O(n), Space: O(n), Space: O(n)

In this example, we’ll be creating the concept of a Hashmap that stores the values we have calculated, so we don’t need to figure them out (the notion of memorization) over and over.

We’ve created an assister function that makes the call recursively.

Our base cases are n1; we return one and equal 2; we return 2.

The time difficulty of this method is O(n) because of memorization.

Space complexity for this solution is O(n) because of the recursive stack usage.

Time to run: 1 ms quicker than 100.00 per cent of Java online submissions for 70. climbing stairs.

Memory Utilization: 33.2 MB, less than 5.26 per cent from Java online submissions of Climbing Steps.

# Solution 2: Bottom-Up Approach, using variables — Time O(n), Space: O(1)

This method will employ the bottom-up approach to reduce the space-related complexity by O(n) down to O(1). What we require are three variables and an infinite loop.

The complexity of time for this method will be O(n).

The complexity of space in this method can be described as O(1).

While you might observe that the LeetCode memory usage results are not showing any improvement in terms of complexity, it offers an impressive improvement between O(n) and O(1).

Running time: zero milliseconds more than 100.00 per cent of Java online submissions for 70. climbing stairs.

Memory Utilization: 33.1 MB, less than 5.26 per cent from Java online submissions of 70. climbing stairs.

# Solution 3: Using Matrix Multiplication, Binet’s Method — Time: O(logn), Space: O(1)

LeetCode presents an answer using matrix multiplication to reduce the time down to O(logn). In my opinion, however, the interviewer should not think of a solution such as this because these are mathematical concepts and not just programming.

For more information, you can visit this Leetcode website.

The solution below is taken from LeetCode, which is available for your reference.

The time Complexity of This Solution is O(log N) since we are traversing the only bits that are long.

The space complexity in this method will be O(1).

Running time: zero milliseconds quicker than 100.00 per cent of Java online submissions for 70. climbing stairs.

Memory Use: 33 MB less than 5.26 per cent from Java online submissions of Climbing Steps.

# Solution 4: Using Formula — Time: O(1), Space: O(1)

LeetCode offers an even more efficient solution that uses the Fibonacci number formula. Again, I believe interviewers will not expect you to know this because it’s solely mathematical. If they are looking for this answer, they might need to assist you with the formula.

Please consult LeetCode for further details.

The solution below comes from LeetCode, which is available for your reference.

The time complexity of this formula is O(1) because we’re merely applying the formula.

The Space Complexity of this solution is O(1).

Time to run: 0. milliseconds quicker than 100.00 per cent of Java online submissions for climbing Stairs.

Memory Utilization: 33.2 MB, less than 5.26 per cent from Java Online submissions to 70. climbing stairs.