<p>You are given an integer array, <code>nums</code>, and an integer <code>k</code>. <code>nums</code> comprises of only <code>0</code>&#39;s and <code>1</code>&#39;s. In one move, you can choose two <strong>adjacent</strong> indices and swap their values.</p>

<p>Return <em>the <strong>minimum</strong> number of moves required so that </em><code>nums</code><em> has </em><code>k</code><em> <strong>consecutive</strong> </em><code>1</code><em>&#39;s</em>.</p>

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

<pre>
<strong>Input:</strong> nums = [1,0,0,1,0,1], k = 2
<strong>Output:</strong> 1
<strong>Explanation:</strong> In 1 move, nums could be [1,0,0,0,<u>1</u>,<u>1</u>] and have 2 consecutive 1&#39;s.
</pre>

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

<pre>
<strong>Input:</strong> nums = [1,0,0,0,0,0,1,1], k = 3
<strong>Output:</strong> 5
<strong>Explanation:</strong> In 5 moves, the leftmost 1 can be shifted right until nums = [0,0,0,0,0,<u>1</u>,<u>1</u>,<u>1</u>].
</pre>

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

<pre>
<strong>Input:</strong> nums = [1,1,0,1], k = 2
<strong>Output:</strong> 0
<strong>Explanation:</strong> nums already has 2 consecutive 1&#39;s.
</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>nums[i]</code> is <code>0</code> or <code>1</code>.</li>
	<li><code>1 &lt;= k &lt;= sum(nums)</code></li>
</ul>

class Solution:
    def minMoves(self, nums: List[int], k: int) -> int:
        # We can count the gaps between 1's. If there are N 1's, the gap array has the length of N-1.
        # gap = [...] where gap[i] denotes the gap between the i-th 1 and (i+1)-th 1.
        # Given k, we want to find a k-1 window in gap array: gap[m], gap[m + 1], ..., gap[m + k - 2].
        # We want to find a point as the central point to merge 1's to make them consecutive.
        # If we choose the leftmost one, the moves could be gap[m] * (k - 1) + ... + gap[m + k - 2] * 1.
        # If we choose the mid one. Consider k - 1 is odd, then the moves could be gap[m] * 1 + ... + gap[m + k // 2 - 1] * (k // 2) + ... + gap[m + k - 2] * 1. We can prove this is the most optimal because other solutions have the factor for each gap[i] not less than the one when we choose the mid one.
        # Consider k - 1 is even, the moves could be gap[m] * 1 + ... + gap[m + (k - 1) // 2 - 1] * (k - 1) // 2 + gap[m + (k - 1) // 2] * (k - 1) // 2 + ... gap[m + k - 2] * 1.
        # When we slide the window, we also have two cases:
        # k - 1 is odd, delta is SUM(m + k // 2, m + k - 1) - SUM(m, m + k // 2 - 1).
        # k - 1 is even, delta is -SUM(m, m + (k - 1) // 2 - 1) + SUM(m + 1 + (k - 1) // 2, m + k - 1).
        # We can leverage prefix sum to accelerate the calculation of SUM().
        if k == 1:
            # k <= sum(nums), hence sum(nums) >= 1 and there are 1's in the input array
            return 0
        prev = None
        gap = []
        for i, num in enumerate(nums):
            if num == 1:
                if prev is not None:
                    gap.append(i - prev - 1)
                prev = i
        # Compute prefix sum for gap array for quick computation of SUM(.).
        ps_gap = list(itertools.accumulate([0] + gap))
        window = 0
        # Initial window: gap[0] ... gap[k - 2]
        if (k - 1) % 2 == 0:
            for i in range((k - 1) // 2):
                window += (i + 1) * (gap[i] + gap[k - 2 - i])
        else:
            for i in range(k // 2 - 1):
                window += (i + 1) * (gap[i] + gap[k - 2 - i])
            window += gap[k // 2 - 1] * (k // 2)
        ans = window
        # Sliding window
        # [1, k - 1], ... [len(gap) - k + 1, len(gap) - 1]
        for j in range(len(gap) - k + 1):
            # window: [j, j + k - 2]
            if (k - 1) % 2 == 0:
                # -SUM(m, m + (k - 1) // 2 - 1) + SUM(m + 1 + (k - 1) // 2, m + k - 1)
                delta = - ps_gap[j + (k - 1) // 2] + ps_gap[j] + ps_gap[j + k] - ps_gap[j + 1 + (k - 1) // 2]
            else:
                # SUM(m + k // 2, m + k - 1) - SUM(m, m + k // 2 - 1)
                delta = ps_gap[j + k] - ps_gap[j + k // 2] - (ps_gap[j + k // 2] - ps_gap[j])
            window += delta
            ans = min(ans, window)
        return ans
            
22200