1871. Jump Game VII

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.

def canReach(self, s: str, minJump: int, maxJump: int) -> bool: 
    if s[-1] == '1': 
        return False 
    
    n = len(s) 
    
    reach = [False] * n 
    
    reach[0] = True 
    cum = 0 
    
    for i in range(1, n): 
        
        if i - maxJump - 1 >= 0: 
            cum -= reach[i - maxJump - 1] 
        if i - minJump >= 0: 
            cum += reach[i - minJump] 
        
        reach[i] = s[i] == '0' and cum > 0 
        
    return reach[-1]