How to Solve Climbing Stairs Problem — Leetcode # 70
Post last modified:December 15, 2022
Reading time:4 mins read
Introduction
In this article we will solve Leetcode #70. This problem is best problem to start learning about dynamic programming. It’s classic!
Problem Statement
We have to climb staircase. we can take up-to n steps to reach top.
Each time we can either take 1 step or 2 step. We need to find out in how many ways we can climb to the top.
Solutions
We can solve this problem using dynamic programming. In dynamic programming our major goal is to break down large problem into subproblems.
So we have to climb N steps , then how about N-1 , how about N-2 ,… how about 2 steps, how about 1 steps and how about 0 steps.
So we bring down to the base of the problem and find answers upwards.
There are 2 ways to solve DP problem , either top down or bottom up. We will solve in both ways.
Solution #1 : (Top Down)
Let’s solve using top down approach. Top down approach is nothing but backtracking/ recursion with some cache/memoization.
Cache/Memoization help us to avoid recompute already computed subproblems.
We first need to figure out what would be the base case. In this problem, if steps that we are taking is more than n , then we can return 0 because then we don’t have any ways to reach top, we are already ahead of top.
If our current steps at top then we can say that we found 1 way to reach to the top.
If our current step is already computed by some other subproblem then we already have that in our cache then just return it instead of making recursion call again.
Once we are done with base cases then we can focus on main logic. At each step we can either take 1 step or 2 step , so we will make backtrack call at index+1 and index+2 step and sum them because these call will return number of ways to reach the top when we take 1 step and when we take 2 step.
The we save this result to our cache , in case if we reach to index again to avoid recomputing.
private int backtrack(int index, int n){
if(index > n) return 0;
if(cache[index] != null) return cache[index];
if(index==n){
return 1;
}
int res = backtrack(index+1, n) + backtrack(index+2, n);
cache[index] = res;
return res;
}
Solution #2 : (Bottom Up)
We will solve using bottom up approach. In this case we initialize cache/dp array with base cases.
So in case when we have 0 steps then we can reach to top in 0 ways. when we have 1 steps then we can reach to the top in 1 way. when we have 2 steps then we can reach to the top in 2 ways.
Now we can use these base cases to form other steps by iterating over loop until n steps.
Basically in order to reach @ step 3 , we can either come from step 2 or step 1. So we add both of them. it takes 1 way to reach step and it takes 2 ways to reach step. hence to reach 3 -> 1+2 = 3 ways.
Similarly @ step 4 = step 3+step 2 = 3+2 = 5 ways and so on.
The cache[N] will store our final result to reach at n step. we can just return it.
We use cookies on our website to give you the most relevant experience by remembering your preferences and repeat visits. By clicking “Accept”, you consent to the use of ALL the cookies.
This website uses cookies to improve your experience while you navigate through the website. Out of these, the cookies that are categorized as necessary are stored on your browser as they are essential for the working of basic functionalities of the website. We also use third-party cookies that help us analyze and understand how you use this website. These cookies will be stored in your browser only with your consent. You also have the option to opt-out of these cookies. But opting out of some of these cookies may affect your browsing experience.
Necessary cookies are absolutely essential for the website to function properly. These cookies ensure basic functionalities and security features of the website, anonymously.
Cookie
Duration
Description
cookielawinfo-checkbox-analytics
11 months
This cookie is set by GDPR Cookie Consent plugin. The cookie is used to store the user consent for the cookies in the category "Analytics".
cookielawinfo-checkbox-functional
11 months
The cookie is set by GDPR cookie consent to record the user consent for the cookies in the category "Functional".
cookielawinfo-checkbox-necessary
11 months
This cookie is set by GDPR Cookie Consent plugin. The cookies is used to store the user consent for the cookies in the category "Necessary".
cookielawinfo-checkbox-others
11 months
This cookie is set by GDPR Cookie Consent plugin. The cookie is used to store the user consent for the cookies in the category "Other.
cookielawinfo-checkbox-performance
11 months
This cookie is set by GDPR Cookie Consent plugin. The cookie is used to store the user consent for the cookies in the category "Performance".
viewed_cookie_policy
11 months
The cookie is set by the GDPR Cookie Consent plugin and is used to store whether or not user has consented to the use of cookies. It does not store any personal data.
Functional cookies help to perform certain functionalities like sharing the content of the website on social media platforms, collect feedbacks, and other third-party features.
Performance cookies are used to understand and analyze the key performance indexes of the website which helps in delivering a better user experience for the visitors.
Analytical cookies are used to understand how visitors interact with the website. These cookies help provide information on metrics the number of visitors, bounce rate, traffic source, etc.
Advertisement cookies are used to provide visitors with relevant ads and marketing campaigns. These cookies track visitors across websites and collect information to provide customized ads.