Intuitive answer with comments
Complexity\n- Time complexity: O(n)\n\n- Space complexity: O(n)\n\n# Code\n\nclass Solution {\n    long long calculateMovesForLeftMostOne(const vector<int>& one
# Complexity\n- Time complexity: $$O(n)$$\n\n- Space complexity: $$O(n)$$\n\n# Code\n```\nclass Solution {\n    long long calculateMovesForLeftMostOne(const vector<int>& ones, int k, int maxChanges, int& rightMostConsumedOne) {\n        // Calculate the number of moves if starting from the left-most 1\n        long long moves = 0;\n        int remainingOnes = k - 1;\n        rightMostConsumedOne = 1;\n\n        // Is there an adjacent 1 to consume before using a change?\n        if (ones.size() > 1 && ones[1] - ones[0] == 1) {\n            ++moves;\n            --remainingOnes;\n            ++rightMostConsumedOne;\n        }\n\n        // Consume as many changes as possible\n        int usedChanges = min(remainingOnes, maxChanges);\n        remainingOnes -= usedChanges;\n        moves += usedChanges * 2;\n\n        // Include more 1s to the right until we have enough\n        for (; remainingOnes > 0; --remainingOnes, ++rightMostConsumedOne) {\n            moves += ones[rightMostConsumedOne] - ones[0];\n        }\n\n        --rightMostConsumedOne;\n        return moves;\n    }\n\npublic:\n    long long minimumMoves(vector<int>& nums, int k, int maxChanges) {\n        vector<int> ones;\n        for (int i = 0; i < nums.size(); ++i) {\n            if (nums[i] == 1)\n                ones.push_back(i);\n        }\n        \n        if (ones.empty())\n            return 2*k; // We have to change and swap for each needed 1\n        \n        if (k == 1) // There exists at least one 1, so we\'d start there and be done without any moves\n            return 0;\n        \n        int leftMost = 0;\n        int rightMost = 1;\n        long long moves = calculateMovesForLeftMostOne(ones, k, maxChanges, rightMost);\n        long long minMoves = moves;\n        \n        // Try every other 1, in order, as a starting position\n        // and shift the window of used 1s if beneficial\n        for (int cur = 1; cur < ones.size(); ++cur)\n        {\n            // Can\'t not include the starting position, so shift the window if necessary\n            if (cur > rightMost)\n            {\n                moves -= ones[cur - 1] - ones[leftMost];\n                ++rightMost;\n                ++leftMost;\n                moves += ones[rightMost] - ones[cur - 1];\n            }\n            \n            // By shifting the dylanIndex, all the 1s to the left increased in cost\n            // and all the 1s to the right decreased in cost\n            int onesToTheLeft = cur - leftMost;\n            int onesToTheRight = rightMost - cur + 1; // includes the starting point 1\n            int delta = ones[cur] - ones[cur - 1];\n            moves += ((long long)onesToTheLeft - onesToTheRight) * delta;\n\n            auto costOfNextOne = [&](){\n                if (rightMost == ones.size() - 1)\n                    return 1000000;\n\n                return ones[rightMost + 1] - ones[cur];\n            };\n            \n            // If the next available 1 to the right is only one away,\n            // we can use this one instead of a change (if there was a change being used)\n            if (costOfNextOne() == 1 && k > (rightMost - leftMost + 1))\n            {\n                --moves;\n                ++rightMost;\n            }\n            \n            int costOfLeftMost = ones[cur] - ones[leftMost];\n            // If the cost of the left most 1 is only one, then it\'s better than a change \n            // and better or equal to any 1 that\'s more to the right than the current right most 1\n            while (costOfLeftMost > 1)\n            {\n                // If we have unused changes, we can convert the left most 1 into a change\n                if (k <= (rightMost - leftMost) + maxChanges)\n                {\n                    ++leftMost;\n                    moves -= costOfLeftMost - 2;\n                }\n                else // We check the cost of replacing left most 1 with the next right 1\n                {\n                    int nextRightCost = costOfNextOne();\n                    if (nextRightCost < costOfLeftMost)\n                    {\n                        ++rightMost;\n                        ++leftMost;\n                        moves -= costOfLeftMost - nextRightCost;\n                    }\n                    else\n                    {\n                        // It\'s better to stay with the current 1s,\n                        // so we are done optimizing this starting position\n                        break;\n                    }\n                }\n                costOfLeftMost = ones[cur] - ones[leftMost];\n            }\n            \n            minMoves = min(minMoves, moves);\n        }\n        \n        return minMoves;\n    }\n};\n```
['C++']