<p>You are given a binary array <code>nums</code> and an integer <code>k</code>.</p>

<p>A <strong>k-bit flip</strong> is choosing a <strong>subarray</strong> of length <code>k</code> from <code>nums</code> and simultaneously changing every <code>0</code> in the subarray to <code>1</code>, and every <code>1</code> in the subarray to <code>0</code>.</p>

<p>Return <em>the minimum number of <strong>k-bit flips</strong> required so that there is no </em><code>0</code><em> in the array</em>. If it is not possible, return <code>-1</code>.</p>

<p>A <strong>subarray</strong> is a <strong>contiguous</strong> part of an array.</p>

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

<pre>
<strong>Input:</strong> nums = [0,1,0], k = 1
<strong>Output:</strong> 2
<strong>Explanation:</strong> Flip nums[0], then flip nums[2].
</pre>

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

<pre>
<strong>Input:</strong> nums = [1,1,0], k = 2
<strong>Output:</strong> -1
<strong>Explanation:</strong> No matter how we flip subarrays of size 2, we cannot make the array become [1,1,1].
</pre>

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

<pre>
<strong>Input:</strong> nums = [0,0,0,1,0,1,1,0], k = 3
<strong>Output:</strong> 3
<strong>Explanation:</strong> 
Flip nums[0],nums[1],nums[2]: nums becomes [1,1,1,1,0,1,1,0]
Flip nums[4],nums[5],nums[6]: nums becomes [1,1,1,1,1,0,0,0]
Flip nums[5],nums[6],nums[7]: nums becomes [1,1,1,1,1,1,1,1]
</pre>

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

<ul>
	<li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li>
	<li><code>1 &lt;= k &lt;= nums.length</code></li>
</ul>

class Solution:
    """
    Naive:
    init queue with first k elements.
    count = 0
    for i in range(k, n - k):
        top = queue.pop()
        if top == 0:
            negated all elements in queue
            count += 1
        queue.append(arr[i])
    if queue is all 0s:
        return count + 1
    elif queue is all 1:
        return count
    else:
        return -1

    Improvement:
    Instead of keeping a queue for switching, we can keep record of the current effect and it's duration.
    Keep a negation flag, and a counter for its effect.
    Whenever a 0 is encountered, it negates the flag and refreshes the counter to k - 1.
    If the counter reaches 0, the flag negates as well.
    The next value will be read according to the negation flag.

    Algorithm:
    negate = False
    counter = float('inf')
    flip_count
    for i in range(n - k):
        val = arr[i] if not negate else 1 - arr[i]
        if val == 0:
            flip_count += 1
            negate = not negate
            counter = k - 1

    ones_count = 0
    for i in range(n - k, n):
        val = arr[i] if not negate else 1 - arr[i]
        ones_count += val

    if ones_count == 0:
        return flip_counter + 1
    if ones_count == k:
        return flip_counter
    return -1

    time complexity: O(n)
    space complexity: O(1)
    """

    """
    k = 3
    nums = [0, 0, 0, 1, 0, 1, 1, 0]

    """
    def minKBitFlips(self, nums: List[int], k: int) -> int:
        n = len(nums)
        flip_count = 0
        negate = False
        for i in range(n - k + 1):
            if i >= k and nums[i - k] == 2:
                negate = not negate

            val = 1 - nums[i] if negate else nums[i]
            if val == 0:
                flip_count += 1
                negate = not negate
                nums[i] = 2

        # Count ones
        ones_count = 1
        for i in range(n - k + 1, n):
            if i >= k and nums[i - k] == 2:
                negate = not negate

            val = 1 - nums[i] if negate else nums[i]
            ones_count += val

        if ones_count == 0:
            return flip_count + 1
        if ones_count == k:
            return flip_count
        return -1


19500