There are n stairs, a person standing at the bottom wants to reach the top. The person can climb either 1 stair or 2 stairs at a time. Count the number of ways, the person can reach the top. Consider the example shown in the diagram. The value of n is 3. There are 3 ways to reach the top. The diagram is taken from Easier Fibonacci puzzles Examples: Input: n = 1 Output: 1 There is only one way to climb 1 stair Input: n = 2 Output: 2 There are two ways: (1, 1) and (2) Input: n = 4 Output: 5 (1, 1, 1, 1), (1, 1, 2), (2, 1, 1), (1, 2, 1), (2, 2)Method 1: The first method uses the technique of recursion to solve this problem. The above expression is actually the expression for Fibonacci numbers, but there is one thing to notice, the value of ways(n) is equal to fibonacci(n+1). ways(1) = fib(2) = 1 ways(2) = fib(3) = 2 ways(3) = fib(4) = 3For a better understanding, let’s refer to the recursion tree below -: Input: N = 4 fib(5) '3' / \ '2' / \ fib(4) fib(3) '2' / \ '1' / \ / \ / \ fib(3) fib(2)fib(2) fib(1) / \ '1' / \ '0' '1' / '1'\ / \ / \ fib(1) fib(0) fib(2) fib(1)So we can use the function for Fibonacci numbers to find the value of ways(n). Following is C++ implementation of the above idea.
Complexity Analysis:
Generalization of the Problem Approach: For the generalization of above approach the following recursive relation can be used. ways(n, m) = ways(n-1, m) + ways(n-2, m) + ... ways(n-m, m)In this approach to reach nth stair, try climbing all possible number of stairs lesser than equal to n from present stair. Following is the implementation of above recurrence.
Complexity Analysis:
Method 2: Memoization We can use the bottom-up approach of dp to solve this problem as well For this, we can create an array dp[] and initialize it with -1. Whenever we see that a subproblem is not solved we can call the recursive method, else we stop the recursion if that the subproblem is solved already.
Complexity Analysis: Time Complexity: O(n) Auxiliary Space: O(n) Method 3: This method uses the technique of Dynamic Programming to arrive at the solution. Approach: We create a table res[] in bottom up manner using the following relation: res[i] = res[i] + res[i-j] for every (i-j) >= 0such that the ith index of the array will contain the number of ways required to reach the ith step considering all the possibilities of climbing (i.e. from 1 to i). Below code implements the above approach:
Complexity Analysis:
Method 4: This method uses the Dynamic Programming Approach with the Space Optimization Approach: In this Method, we can just optimize the Tabular Approach of Dynamic Programming by not using any extra space. First, we can create two variables prev and prev2 to store the ways to climb one stair or two stairs. Then we can run a for loop to count the total number of ways to reach the top. Below is the code implementation of the Above idea:
output: given n=4 Number of ways: 5 Complexity Analysis :
Method 5: The third method uses the technique of Sliding Window to arrive at the solution. Below code implements the above idea
OutputNumber of ways = 13 Complexity Analysis:
This article is contributed by Abhishek. Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above Method 6: The fourth method uses simple mathematics but this is only applicable for this problem if (Order does not matter) while counting steps. Approach: In This method we simply count the number of sets having 2.
OutputNumber of ways when order of steps does not matter is : 3 Complexity Analysis:
Note: This Method is only applicable for the question Count ways to N’th Stair(Order does not matter) . Order does not matter means for n = 4 {1 2 1} ,{2 1 1} , {1 1 2} are considered same. Method 6: This method uses the technique of Matrix Exponentiation to arrive at the solution. Approach: The number of ways to reach nth stair (Order matters) is equal to the sum of number of ways to reach (n-1)th stair and (n-2)th stair This corresponds to the following recurrence relation: f(n) = f(n-1) + f(n-2) f(1) = 1 f(2) = 2where f(n) indicates the number of ways to reach nth stair Note: f(1) = 1 because there is only 1 way to reach n=1 stair {1} f(2) = 2 because there are 2 ways to reach n=2 stairs {1,1} , {2} It is a type of linear recurrence relation with constant coefficients and we can solve them using Matrix Exponentiation method which basically finds a transformation matrix for a given recurrence relation and repeatedly applies this transformation to a base vector to arrive at the solution). F(n) = CN-1F(1) where C is the transformation matrix F(1) is the base vector F(n) is the desired solutionSo, for our case the transformation matrix C would be: CN-1 can be calculated using Divide and Conquer technique, in O( (K^3) Log n) where K is dimension of C And F(1): As an example, For n= 4: F(4) = C3F(1) C3 = Hence, C3F(1) =
Complexity Analysis:
Generalization of the Problem: Given an array A {a1, a2, …., am} containing all valid steps, compute the number of ways to reach nth stair. (Order does matter) Examples: Input: A = [1,2] n = 4 Output: 5 Explanation: This is the given problem, i.e, count the number of ways to reach n=4 stairs with climbing 1 or 2 stairs at a time Therefore, ways will be: {1,1,1,1} {1,1,2} {1,2,1} {2,1,1} {2,2} = 5 Input: A = [2,4,5] n = 9 Output: 5 Explanation: There are 5 ways to reach n=9 stairs with climbing 2 or 4 or 5 stairs at a time Therefore, ways will be: {5,4} {4,5} {2,2,5} {2,5,2} {5,2,2} = 5Approach: The number of ways to reach nth stair is given by the following recurrence relation Let K be the largest element in A. Step1: Calculate base vector F(1) ( consisting of f(1) …. f(K) ) It can be done in O(m2K) time using dynamic programming approach as follows: Let’s take A = {2,4,5} as an example. To calculate F(1) = { f(1), f(2), f(3), f(4), f(5) } we will maintain an initially empty array and iteratively append Ai to it and for each Ai we will find the number of ways to reach [Ai-1, to Ai,] Hence for A = {2 ,4 ,5} Let T be the initially empty array Iteration 1: T = {2} n = {1,2} dp = {0,1} (Number of ways to reach n=1,2 with steps given by T) Iteration 2: T = {2,4} n = {1,2,3,4} dp = {0,1,0,2} (Number of ways to reach n=1,2,3,4 with steps given by T) Iteration 3: T = {2,4,5} n = {1,2,3,4,5} dp = {0,1,0,2,1} (Number of ways to reach n=1,2,3,4,5 with steps given by T)Note: Since some values are already calculated (1,2 for Iteration 2, etc.) we can avoid them in loop After all iterations, the dp array would be: [0,1,0,2,1] Thus, base vector F(1) for A = [2,4,5] is: Now that we have the base vector F(1), calculation of C (Transformation matrix) is easy Step 2: Calculate C, the transformation matrix It is a matrix having elements Ai,i+1= 1 and last row contains constants Now constants can be determined by the presence of that element in A So for A = [2,4,5] constants will be c = [1,1,0,1,0] (Ci = 1 if (K-i+1) is present in A, or else 0 where 1 <= i <= K ) Thus, Transformation matrix C for A =[2,4,5] is:
Step 3: Calculate F(n) To calculate F(n), following formula is used: F(n) = Cn-1F(1)Now that we have C and F(1) we can use Divide and Conquer technique to find Cn-1 and hence the desired output
Complexity Analysis: Time Complexity: O( m2K + K3Logn ) where m is the size of Array A K is the largest element in A n denotes the stair number (nth stair) Space Complexity: O(K2)Note: This approach is ideal when n is too large for iteration For Example: Consider this approach when (1 ≤ n ≤ 109) and (1 ≤ m,k ≤ 102) |