Given a binary string str of length N and an integer K, the task is to find the minimum number of steps required to move from str to str[N – 1] with the following moves:
- From an index i, the only valid moves are i + 1, i + 2 and i + K
- An index i can only be visited if str[i] = ‘1’
Input: str = “101000011”, K = 5
str -> str -> str -> str
Input: str = “1100000100111”, K = 6
There is no possible path.
Input: str = “10101010101111010101”, K = 4
Approach: The idea is to use dynamic programming to solve the problem.
- It is given that for any index i, it is possible to move to an index i+1, i+2 or i+K.
- One of the three possibilities will give the required result that is the minimum number of steps to reach the end.
- Therefore, the dp array is created and is filled in a bottom-up manner.
Below is the implementation of the above approach:
Time Complexity: O(N) where N is the length of the string.
- Find minimum steps required to reach the end of a matrix | Set - 1
- Find minimum steps required to reach the end of a matrix | Set 2
- Minimum steps to reach a destination
- Minimum steps to reach target by a Knight | Set 2
- Minimum steps to reach any of the boundary edges of a matrix | Set 1
- Minimum steps to reach end from start by performing multiplication and mod operations with array elements
- Minimize the number of steps required to reach the end of the array
- Minimum steps to remove substring 010 from a binary string
- Minimum steps to convert one binary string to other only using negation
- Minimum moves to reach from i to j in a cyclic string
- Minimum steps to delete a string by deleting substring comprising of same characters
- Minimum steps to delete a string after repeated deletion of palindrome substrings
- Minimum number of jumps to reach end
- Find the minimum cost to reach destination using a train
- Minimum number of jumps to reach end | Set 2 (O(n) solution)
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.
Improved By : AnkitRai01