Sliding Window
Intuition\n Describe your first thoughts on how to solve this problem. \n- We can notice that obtaining a one using the first type of operation takes exactly 2 
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n- We can notice that obtaining a one using the first type of operation takes exactly 2 moves. (One move for generating a one at an adjacent position and the other move for retrieving it).\n- For any position $i$ where $\\mid \\text{dylanIndex} - i\\mid \\:> 1$ we need more than 1 moves for retrieving it.\n- Hence, ones at positions $\\text{dynlanIndex} + 1$ or $\\text{dynlanIndex} - 1$ are the only ones which require $1$ move and the one at $\\text{dylanIndex}$ needs $0$ moves. Thus, we can always greedily choose to use the first type of operation except for the ones at position $\\text{dynlanIndex} + 1$, $\\text{dynlanIndex} - 1$ and $\\text{dynlanIndex}$.\n- We can choose to ignore either 0 or 1 or 2 or 3 of the first type of operations. We can simply brute force and try all of these options.\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity: $O(n)$\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:$O(n)$\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```python3\n\nINF = 10 ** 10\n\nclass Solution:\n    def getMoves(self, ones: List[int], window: int) -> int:\n        n = len(ones)\n        \n        # No ones to be selected!\n        if window == 0:\n            return 0\n        \n        # Not enough ones to form a window\n        if n < window:\n            return INF \n        \n        leftSize = window // 2\n        rightSize = window - leftSize\n\n        leftSum, rightSum = 0, 0\n        leftQ, rightQ = deque(), deque()\n        \n        def insertRight(ind):\n            nonlocal rightSum\n            rightQ.append(ind)\n            rightSum += ind\n        \n        def insertLeft(ind):\n            nonlocal leftSum\n            leftQ.append(ind)\n            leftSum += ind\n            \n        def removeRight():\n            nonlocal rightSum\n            rightSum -= rightQ[0]\n            return rightQ.popleft()\n        \n        def removeLeft():\n            nonlocal leftSum\n            leftSum -= leftQ[0]\n            return leftQ.popleft()\n        \n        def rebalance():\n            if len(leftQ) <= leftSize and len(rightQ) <= rightSize:\n                return\n            \n            if len(rightQ) > rightSize:\n                x = removeRight()\n                insertLeft(x)\n            \n            if len(leftQ) > leftSize:\n                removeLeft()\n        \n        minCost = INF\n        for ind in ones:\n            insertRight(ind)\n            rebalance()\n            if len(leftQ) + len(rightQ) == window:\n                median = rightQ[0]\n                leftCost = len(leftQ) * median - leftSum\n                rightCost = rightSum - len(rightQ) * median\n                minCost = min(minCost, leftCost + rightCost)\n                \n        return minCost\n    \n    def minimumMoves(self, nums: List[int], k: int, maxChanges: int) -> int:\n        maxChanges = min(k, maxChanges)\n        ones  = [i for i, x in enumerate(nums) if x == 1]\n        \n        ans = INF\n        for ignore in range(4):\n            if maxChanges >= ignore:\n                ans = min(ans, self.getMoves(ones, k - maxChanges + ignore) + (maxChanges - ignore) * 2)\n        return ans\n        \n```
['Greedy', 'Sliding Window', 'Python3']