Explained Sliding Window - Python
\nclass Solution:\n    def minMoves(self, nums: List[int], k: int) -> int:\n        pos = [0]\n        for i, num in enumerate(nums):\n            if num == 1: 
```\nclass Solution:\n    def minMoves(self, nums: List[int], k: int) -> int:\n        pos = [0]\n        for i, num in enumerate(nums):\n            if num == 1:  pos.append(i)\n        n = len(pos)\n        prefix = [0] * n\n        for i in range(1, n):\n            prefix[i] = prefix[i - 1] + pos[i]\n        \n        res = inf\n        \n        # sliding window, [i...r] of size k\n        radius = (k-1) // 2\n        for i in range(1, n - (k - 1)):  # make sure r = i + k - 1 < n\n            mid = i + radius\n            r = i + k - 1\n            right = prefix[r] - prefix[mid]\n            left = prefix[mid - 1] - prefix[i - 1]  # because mid-i=radius\n            save = radius * (radius + 1)\n            extra_cost = 0\n            if k % 2 == 0:\n                extra_cost = pos[mid] + (radius + 1)\n            res = min(res, right - left - save - extra_cost)\n            \n        return res\n```
['Sliding Window']