You are climbing a staircase. It takes
Each time you can either climb
Output: 2
Explanation: There are two ways to climb to the top.
1. step 1 + step 1 ( take one step)
2. step 2 ( directly go to second step)
Output: 3
Explanation: There are three ways to climb to the top.
1. step 1 + step 1 + step 1( take one step)
2. step 1 + step 2 ( take step 1 and then take 2 steps)
3. step 2 + step 1 ( take 2 steps and then take 1 step)
Contents
Solution using brute force approach Solution using DP memoization Solution using DP Bottom-Up approach using tabulation Solution using DP Bottom-Up approach memory optimized
In this approach, we will compute all possible outcomes recursively, by taking possibilities of 1 step and 2 steps from current step we are in.
-
We will create a function
climbStairsBruteForce that takesn as input argument. -
Call
climbStairsBruteForce itself with two possible outcomes, i.e. n-1 and n-2, thus reducing number of remaining eteps. - While solving the problem recursively, we can draw two base cases.
-
When
n becomes 0, that means we are at the last step of stair, then return 1. -
If
n becomes less than 0, then return 0, as there will be no way to climb stairs.
-
When
Complexity Analysis:
Time complexity: Above code runs in O(2n) time where
Space complexity: O(n) as we are making recursive loops
From the above brute force approach, we can notice that there are some overlapping problems.
For example, let's say
- We start at 1st step, then we can either take 1 step or 2 steps.
- We reach step 3, by taking either 1 step two times from 1st step, or by taking 2 steps from 1st step.
-
Once we are at step 3, number of ways to reach step 5 will be same whether we take 1 step approach or 2 steps approach from 1st step.
So, this is an overlapping sub problem and the results can be cached.

-
In this approach of memoization, we are going to cache the sub problem results with key as
n and its result into aHashMap . - When the sub problem answer is found in the cache, we simply return that, if not then we call the function recursively and cache it.
Complexity Analysis:
Time complexity: Above code runs in O(n) time where
Space complexity: O(n) as we are making recursive loops
In this approach we will look at solving the problem using Dynamic programming using tabulation caching mechanism.
The time and memory complexity will be same as above solution using memoization,
but we can notice a pattern to improve memory complexity to constant in later solution.
Intution behind this solution is, when we are at

-
We will create a temporary array
int[] dp = new int[n+1] withn+1 in size, because we are going to cache how many ways to reach 0th step as well for convinience. -
Then, initialize
dp[0] = 1 anddp[1] = 1 , reason we add dp[0] = 0 is for our convinience, to reach step 2, we can take 1 step from 1st step or 1 step from 0th step.
Complexity Analysis:
Time complexity: Above code runs in O(n) time where
Space complexity: O(n) since we have a
If we look at above DP solution using tabulation, we can see that we just need previous two values only.
If we are at step
-
In this approach, we will create two variables
prev andcurrent to hold previous values for i-2nd step and i-1st steps respectively. -
Once we calculte number of ways we can reach current
ith step we are looking at usingprev + current , for the next iteration, we will resetprev andcurrent such that new value at currentith step will become new current and previous current value will become new prev.
Complexity Analysis:
Time complexity: Above code runs in O(n) time where
Space complexity: O(1) since we are only using prev, current and a temp variables.
Above implementations source code can be found at