In interviews and coding contests, indirect questions will be asked about the known concepts. In this article, we are going to discuss the leetcode problem of Climbing stairs. The problem might seem new but the logic is quite familiar even for the beginner. After reading the problem statement try to find the logic to solve this problem.
Let us consider you are climbing a staircase. It takes n steps to reach the top. Each time you can either climb 1 or 2 steps. We have to find how many distinct ways can you climb to the top.
Sample Input: n = 3
Sample Output: 3
(Way 1: 1->1->1, Way 2: 2->1, Way 3: 1->2)
Solution for Climbing Stairs Problem
Before we discuss the solution to this problem, let's discuss about dynamic programming.
What is dynamic programming?
Dynamic programming is a technique that divides the problem into parts, finds a solution at each stage, and stores it for future use. It is used to get an optimized solution with the concept of reusing the stored solutions of the sub-parts. The primary use of dynamic programming is to reduce time complexity. We can solve recursive problems with dynamic programming. It will reduce the time complexity from exponential to linear. Let's try to solve this climbing stairs problem with the help of dynamic programming.
The algorithm for Climbing stairs is exactly similar to the Fibonacci series.
- Let n be the number of stairs. If n <=0, then the number of ways to climb the staircase should be zero.
- If n == 1, then there is only way to climb the stair.
- For the numbers greater than 1, we can simply find the solution by adding previous steps i.e., dp[n-1] + dp[n-2]. Create a new array and store the output at each step.
- Then finally return the last value in the array as the output.
Let us consider the value of n is 3. There are 3 ways to reach the top. We use a dynamic programming approach for this problem. We create a new array of steps that store the value at each step. Let us first initialize steps = 1 and steps = 1, then to find steps we have to add previous two values i.e., steps + steps which is 1+1 = 2.
Then to find steps we have to add previous two values i.e., steps + steps which is 1+2 = 3. Now we know that steps[n] is the final output, hence steps value i.e 3 is the required number of ways to climb the stairs. The explanation is illustrated in the following diagram.
Java Code for Climbing Stairs Problem
C++ Code for Climbing Stairs Problem
Python Code for Climbing Stairs Problem
We are iterating through a single loop to find a number of ways to climb the stairs. Hence the time complexity will be O(n), where n is the number of stairs.
In this approach of storing the values using dynamic programming, we are creating a new array. Hence the space complexity for this solution will be O(n), where n is the number of stairs.
Dynamic programming is an important concept to be covered for any interview or competitive programming. We can also solve this problem with a recursive approach but dynamic programming is much more efficient compared to recursion. There are many ways you can try to solve this problem so you will have a good understanding of the concepts.