You are given a 0-indexed binary string s and two integers minJump and maxJump.

In the beginning, you are standing at index 0, which is equal to '0'. You can move from index i to index j if the following conditions are fulfilled:

  • i + minJump <= j <= min(i + maxJump, s.length - 1), and
  • s[j] == '0'

Return true if you can reach index s.length - 1 in s, or false otherwise.

Input & Output

Example 1 — Basic Jump Sequence
$ Input: s = "011010", minJump = 2, maxJump = 3
Output: true
💡 Note: Start at index 0, jump to index 3 (jump of size 3), then jump to index 5 (jump of size 2). Both positions contain '0' and are within jump constraints.
Example 2 — Blocked Path
$ Input: s = "01101110", minJump = 2, maxJump = 3
Output: false
💡 Note: From index 0, we can reach index 2 or 3. From index 3, we can reach indices 5 or 6, but both contain '1'. No valid path to the last index.
Example 3 — Minimum Jump Only
$ Input: s = "0101", minJump = 1, maxJump = 1
Output: false
💡 Note: From index 0, jump to index 1 ('1' - invalid). Cannot proceed further, so cannot reach index 3.

Constraints

  • 2 ≤ s.length ≤ 105
  • s[i] is either '0' or '1'
  • s[0] == '0'
  • 1 ≤ minJump ≤ maxJump < s.length

Visualization

Tap to expand
Jump Game VII - Optimal Solution INPUT Binary String s: 0 i=0 1 i=1 1 i=2 0 i=3 1 i=4 0 i=5 = Can land ('0') = Blocked ('1') Parameters: minJump = 2 maxJump = 3 From index i, can jump to: [i+2, i+3] if s[j]='0' Start: index 0 Goal: index 5 ALGORITHM STEPS 1 Initialize dp[0]=true, track reachable 2 Sliding Window Count reachable in [i-max, i-min] 3 Check Conditions s[i]='0' AND count > 0 4 Mark Reachable dp[i]=true, update count DP Array (reachable): T F F T F T Jump Path: 0 ---> 3 ---> 5 +3 +2 FINAL RESULT 0 START 1 1 0 STEP 1 0 GOAL Output: true Path Found: 0 ---> 3 (jump +3) 3 ---> 5 (jump +2) Reached end: OK Key Insight: Use sliding window with DP to efficiently track reachable positions. Instead of checking all previous indices for each position, maintain a count of reachable positions in the jump range. Time: O(n) | Space: O(n) - optimal compared to O(n*maxJump) brute force TutorialsPoint - Jump Game VII | Sliding Window + DP Approach
Asked in
Meta 25 Google 20 Amazon 18 Microsoft 15
32.0K Views
Medium Frequency
~25 min Avg. Time
890 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen