70. Climbing Stairs

Problem

Intuition

It is a linear DP problem. The question asked is to find the number of unique ways to reach to "nth" step. From every step we can move forward "1" or "2" steps and should be inside the index limits.

Since they have asked for number of unique paths, we need to explore all the paths. To explore all the paths, we need to find a recursive way.

But using recursive way we might hit TLE as it does not scale with larger steps and the space will be a bottleneck. This because we might be calculating the same problem more than once, which is not needed.

Top Down

To solve this issue we can introduce a cache called dp arraywhere we can store the computed values.

Bottom Up

In this method, we start from 1st step and try to solve the problems for each cell what would be the number of unique paths at each cell and using that we will be solving till "nth" step

Time and SpaceComplexity

Approach
Time Complexity
Space Complexity

Tabulation with Space optimization (Bottom Up)

O(n)

O(1)

Tabulation (Bottom Up)

O(n)

O(n)

Memoization (Top Down)

O(n)

O(h) + O(n)

Recursive

O(2n)O(2^n)

O(n)

Solution

Last updated