Given a distance N. The task is to count the total number of ways to cover the distance with 1, 2 and 3 steps.
Input: N = 3
All the required ways are (1 + 1 + 1), (1 + 2), (2 + 1) and (3).
Input: N = 4
Approach: In previous article, a recursive and dynamic programming based approach has been discussed. Here we will reduce the space complexity. It can be onserved that to calculate the number of steps to cover the distance i, only the last three states are required (i – 1, i – 2, i – 3). So, the result can be calculated using the last three states.
Below is the implementation of the above approach:
Time Complexity: O(N)
Space Complexity O(1)
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Count number of ways to cover a distance
- Count minimum number of fountains to be activated to cover the entire garden
- Vertex Cover Problem | Set 2 (Dynamic Programming Solution for Tree)
- Number of squares of side length required to cover an N*M rectangle
- Minimum number of points required to cover all blocks of a 2-D grid
- Maximum number of tiles required to cover the floor of given size using 2x1 size tiles
- Distance of chord from center when distance between center and another equal length chord is given
- Minimum steps needed to cover a sequence of points on an infinite grid
- Minimum squares to cover a rectangle
- Minimum cost to cover the given positions in a N*M grid
- Minimum time required to cover a Binary Array
- Print all possible ways to convert one string into another string | Edit-Distance
- Count number of ways to partition a set into k subsets
- Count number of ways to divide a number in 4 parts
- Count ways to split a Binary String into three substrings having equal count of zeros
- Bitmasking and Dynamic Programming | Set 1 (Count ways to assign unique cap to every person)
- Count ways to divide circle using N non-intersecting chord | Set-2
- Count the number of ways to tile the floor of size n x m using 1 x m size tiles
- Count number of ways to reach a given score in a game
- Count number of ways to fill a "n x 4" grid using "1 x 4" tiles
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. 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.