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 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 number of jumps to reach end | Set 2 (O(n) solution)
- Minimum number of Fibonacci jumps to reach end
- Paths requiring minimum number of jumps to reach end of array
- Minimum number of jumps to reach end
- 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 a destination
- Minimize steps defined by a string required to reach the destination from a given source
- Minimum cost to reach end of array array when a maximum jump of K index is allowed
- Count minimum factor jumps required to reach the end of an Array
- Maximum power of jump required to reach the end of string
- Count number of steps to cover a distance if steps can be taken in powers of 2
- Find if it is possible to reach the end through given transitions
- Count number of ways to jump to reach end
- Number of ways to reach the end of matrix with non-zero AND value
- Expected number of moves to reach the end of a board | Dynamic programming
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.
Improved By : AnkitRai01