1871. Jump Game VII
Jump Game VII (LeetCode 1871) is a Medium Data Structures and Algorithms interview problem you can solve, run and submit on Thita.ai. It belongs to the Sliding Window pattern, in the Variable Size (Condition-Based) subpattern.
How to solve Jump Game VII
Jump Game VII is a Variable Size Sliding Window problem, part of Sliding Window. Master variable-size sliding window for condition-based problems like longest substring without repeating characters.
Work towards the solution in these steps:
- Track which positions are reachable as you scan from left to right.
- A position can be reached only if it is `'0'` and there is a reachable earlier position at a distance from `minJump` through `maxJump`.
- For each destination index, the possible predecessor indices form a sliding interval.
The reference solution runs in O(n) time and O(n) space.
The Jump Game VII solution and editorial walks through the full approach with reference code.
Problem statement
You are given a binary string s, with positions numbered from 0 through s.length - 1. Position 0 is the starting position, and the destination is the final position, s.length - 1. A position is open when its character is '0'; a position containing '1' cannot be landed on.
From a position i, you may move forward to a position j only when minJump <= j - i <= maxJump. Every position on which you land must be open. The starting position is already occupied, so it does not require a jump.
Implement canReach and return true if the destination can be reached from position 0; otherwise, return false.
Example 1:
Input: s = "00100100", minJump = 2, maxJump = 3 Output: false Explanation: Position 3 is reachable from position 0, and position 6 is then reachable from position 3. However, the destination at position 7 is four steps from position 3 and one step from position 6, so it cannot be reached with jumps of length 2 through 3.
Example 2:
Input: s = "0101100", minJump = 2, maxJump = 3 Output: false Explanation: Position 2 is reachable from position 0, but the next possible positions contain '1' or lead past the destination. Thus the final position cannot be reached.
Example 3:
Input: s = "0000000", minJump = 3, maxJump = 4 Output: true Explanation: A valid route is position 0 to position 3, followed by position 3 to the destination at position 6.
Constraints:
- 2 <= s.length <= 10^5 - s consists only of the characters '0' and '1' - s[0] == '0' - 1 <= minJump <= maxJump < s.length
Topics and companies
Topics: Array, String, Sliding Window, Dynamic Programming.
How to practise Jump Game VII on Thita.ai
Starter code is provided in C#, C++, Go, Java, JavaScript and Python. Your solution runs against 5 test cases for this problem, with the AI coach available for a hint when you are stuck rather than a finished answer. The reference solution runs in O(n) time and O(n) space.
Open Jump Game VII in the code editor, or read the Jump Game VII editorial for a worked solution with its approach and complexity analysis.
Where Jump Game VII sits in the DSA pattern sheet
Problems related to Jump Game VII
Other problems that use the same Variable Size (Condition-Based) and Sliding Window techniques:
- Contains Duplicate II — Easy, same subpattern
- Continuous Subarrays — Medium, same subpattern
- Find Longest Special Substring That Occurs Thrice I — Medium, same subpattern
- Frequency of the Most Frequent Element — Medium, same subpattern
- Fruit Into Baskets — Medium, same subpattern
- Longest Continuous Subarray With Absolute Diff Less Than or Equal to Limit — Medium, same subpattern
Jump Game VII FAQ
What is the LeetCode number of Jump Game VII?
Jump Game VII is LeetCode problem 1871, rated Medium.
What pattern is used to solve Jump Game VII?
Jump Game VII is a Variable Size Sliding Window problem. It belongs to the Sliding Window pattern, in the Variable Size (Condition-Based) subpattern.
What is the time complexity of Jump Game VII?
The reference solution runs in O(n) time and O(n) space.