Explained Sliding window 3ms
IntuitionThe key insight is that we can collect ones in three ways with different costs:
Cost 0: Taking a 1 from current position
Cost 1: Swapping with adjacent
# Intuition
The key insight is that we can collect ones in three ways with different costs:
1. Cost 0: Taking a 1 from current position
2. Cost 1: Swapping with adjacent 1s 
3. Cost 2: Creating new 1s using maxChanges allowance

# Approach
We use a sliding window approach combined with greedy optimization:

At each potential starting position i:
1. Calculate how many ones we can get locally:
   - From current position (cost0_count)
   - From adjacent positions through swaps (cost1_count) 
   - From creating new ones via maxChanges (from_changes_cost2_count)

2. If local operations can satisfy k, update result with their total cost.

3. Otherwise, maintain two windows around position i:
   - Left window [beg, i] tracking ones we passed
   - Right window (i, end) tracking upcoming ones
   
4. The windows are optimized by:
   - Expanding right window until we have enough ones
   - Replacing far ones from left with closer ones from right when beneficial
   
5. Update result with combined cost of:
   - Local operations (cost1_count + 2 * from_changes_cost2_count)
   - Movement costs from both windows (left_cost + right_cost)

# Complexity
- Time complexity: $$O(n)$$ where n is length of nums

# Code
```cpp []
class Solution {
public:
    long long minimumMoves(const vector<int>& nums, int k, int maxChanges) {
        long long res = numeric_limits<long>::max();

        // Left window: [beg..i] ; beg: [0..i)
        // right window: (i..end) ; end: (i..N)
        long long beg = 0, left_count = 0, left_cost = 0;
        long long end = 1, right_count = nums[0], right_cost = 0;

        for (int i = 0; i < nums.size(); ++i,
                                         // Move right: left window becomes more expensive
                                         left_cost += left_count, right_cost -= right_count) {
            if (nums[i]) {
                ++left_count;
                --right_count;
                end += end == i; // Extend window if needed
            }

            const int cost0_count = nums[i]; // For 0 cost we can get where we stay
            const int cost1_count = min( // For cost 1 we can get from neighbors
                k - cost0_count,
                (i > 0 && nums[i - 1]) + (i < nums.size() - 1 && nums[i + 1]));
            // For cost 2 we can use maxChanges
            const int from_changes_cost2_count = min(maxChanges, k - cost0_count - cost1_count);
            // Finally we know how many we get from existing array including cost0 and cost1
            const int from_nums_req = k - from_changes_cost2_count;

            // If we can satisfy k from cheapest local operations
            if (k - cost0_count - cost1_count - from_changes_cost2_count == 0) {
                res = min<long long>(res, cost1_count + 2 * from_changes_cost2_count);
                continue;
            }

            // Expand right window until we have enough ones
            while (end < nums.size() && left_count + right_count < from_nums_req) {
                right_count += nums[end];
                right_cost += nums[end] * (end - i);
                ++end;
            }

            // Skip zeros
            for (; end < nums.size() && nums[end] == 0; ++end);
            for (; beg < i && nums[beg] == 0; ++beg);

            // Optimize window by replacing far ones (from left) with closer ones (from right)
            while (end < nums.size() && beg < i && nums[beg] && end - i < i - beg) {
                // Remove from left side
                --left_count;
                left_cost -= i - beg;
                ++beg;
                for (; beg < i && nums[beg] == 0; ++beg);

                // Add to right side
                ++right_count;
                right_cost += end - i;
                ++end;
                for (; end < nums.size() && nums[end] == 0; ++end);
            }

            res = min(res, 2 * from_changes_cost2_count + left_cost + right_cost);
        }

        return res;
    }
};

```
['C++']