Given N number of stairs. Also given the number of steps that one can cover at most in one leap (K). The task is to find the number of possible ways one (only consider combinations) can climb to the top of the building in K leaps or less from the ground floor.
Input: N = 5, K = 3
To reach stair no-5 we can choose following combination of leaps:
1 1 1 1 1
1 1 1 2
1 2 2
1 1 3
Therefore the answer is 5.
Input: N = 29, K = 5
Let combo[i] be the number of ways to reach the i-th floor. Hence the number of ways to reach combo[i] from combo[j] by taking a leap of i-j will be combo[i] += combo[j]. So iterate for all possible leaps, and for each possible leaps keep adding the possible combinations to the combo array. The final answer will be stored in combo[N].
Below is the implementation of the above approach.
Time Complexity: O(N*K)
Auxiliary Space: O(N)
GeeksforGeeks has prepared a complete interview preparation course with premium videos, theory, practice problems, TA support and many more features. Please refer Placement 100 for details
- Count ways to reach Nth Stairs by taking 1 and 2 steps with exactly one 3 step
- Number of ways to reach the end of matrix with non-zero AND value
- Number of ways to arrange K different objects taking N objects at a time
- Count number of ways to jump to reach end
- Count number of ways to reach a given score in a game
- Count number of ways to reach destination in a Maze
- Count number of ways to reach a given score in a Matrix
- Number of ways to reach (X, Y) in a matrix starting from the origin
- Find the number of ways to reach Kth step in stair case
- Count the number of ways to tile the floor of size n x m using 1 x m size tiles
- Minimum cost to reach the top of the floor by climbing stairs
- Count ways to reach the n'th stair
- Count ways to reach a score using 1 and 2 with no consecutive 2s
- Count ways to reach the nth stair using step 1, 2 or 3
- Count ways to reach end from start stone with at most K jumps at each step
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.