<p>You are given a <strong>0-indexed</strong> binary string <code>s</code> and two integers <code>minJump</code> and <code>maxJump</code>. In the beginning, you are standing at index <code>0</code>, which is equal to <code>&#39;0&#39;</code>. You can move from index <code>i</code> to index <code>j</code> if the following conditions are fulfilled:</p>

<ul>
	<li><code>i + minJump &lt;= j &lt;= min(i + maxJump, s.length - 1)</code>, and</li>
	<li><code>s[j] == &#39;0&#39;</code>.</li>
</ul>

<p>Return <code>true</code><i> if you can reach index </i><code>s.length - 1</code><i> in </i><code>s</code><em>, or </em><code>false</code><em> otherwise.</em></p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>

<pre>
<strong>Input:</strong> s = &quot;<u>0</u>11<u>0</u>1<u>0</u>&quot;, minJump = 2, maxJump = 3
<strong>Output:</strong> true
<strong>Explanation:</strong>
In the first step, move from index 0 to index 3. 
In the second step, move from index 3 to index 5.
</pre>

<p><strong class="example">Example 2:</strong></p>

<pre>
<strong>Input:</strong> s = &quot;01101110&quot;, minJump = 2, maxJump = 3
<strong>Output:</strong> false
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>2 &lt;= s.length &lt;= 10<sup>5</sup></code></li>
	<li><code>s[i]</code> is either <code>&#39;0&#39;</code> or <code>&#39;1&#39;</code>.</li>
	<li><code>s[0] == &#39;0&#39;</code></li>
	<li><code>1 &lt;= minJump &lt;= maxJump &lt; s.length</code></li>
</ul>

class Solution:
    def canReach(self, s: str, minJump: int, maxJump: int) -> bool:
        # One pass dynamic programming with sliding window
        #
        # The general idea is the same as the brute force
        # bottom-up dynamic programming solution, but instead of
        # always looping through all the previous reachable spots,
        # we maintain a partial sum that counts the number of previous 
        # reachable spots in the sliding window [i - maxJump, i - minJump].
        # 
        # O(n) time complexity one pass
        # O(n) space complexity for dp array

        # dp[i] represents whether i is reachable
        dp = [False for _ in s]
        dp[0] = True

        # num_prev_reachable counts the number of previous spots
        # within the sliding window [i - maxJump, i - minJump]
        # that we can jump from to reach i
        num_prev_reachable = 0

        for i in range(1, len(s)):
            # the sliding window left bound is being updated to i - maxJump
            # the sliding window right bound is being updated to i - minJump
            
            # exclude previous reachable spot (prev left bound)
            if i > maxJump and dp[i - maxJump - 1]:
                num_prev_reachable -= 1
            # include new reachable spot (new right bound)
            if i >= minJump and dp[i - minJump]:
                num_prev_reachable += 1
            
            # if we can reach this 0 from prev reachable spots, it is reachable
            if s[i] == "0" and num_prev_reachable > 0:
                dp[i] = True

        return dp[-1]

# class Solution:
#     def canReach(self, s: str, minJump: int, maxJump: int) -> bool:
# 		# dp[i] represents whether i is reachable
#         dp = [False for _ in s]
#         dp[0] = True

#         for i in range(1, len(s)):
#             if s[i] == "1":
#                 continue

# 			# iterate through the solutions in range [i - maxJump, i - minJump]
# 			# and if any previous spot in range is reachable, then i is also reachable
#             window_start = max(0, i - maxJump)
#             window_end = i - minJump + 1
#             for j in range(window_start, window_end):
#                 if dp[j]:
#                     dp[i] = True
#                     break
        
#         return dp[-1]       
18400