Problem Description
Given a binary array nums, an integer k and a limit maxChanges on the number of allowed bit‐flips, Alice wants to “pick up” exactly k ones using the minimum number of moves. When the game begins, she may choose any index (called aliceIndex) from 0 to n–1. If nums[aliceIndex] is 1 then that one is picked instantly (and the cell becomes 0) without consuming a move. Afterwards, in each move she can perform exactly one of the following actions: • Pick any index j (j != aliceIndex) with nums[j] == 0 and change it to 1. This “flip” action can be used at most maxChanges times. • Pick two adjacent indices x and y (|x – y| == 1) with nums[x] == 1 and nums[y] == 0, and swap them – that is, set nums[y] = 1 and nums[x] = 0. If y equals aliceIndex then the one arriving there is immediately “picked up” (and that cell becomes 0). Return the minimum number of moves required to achieve exactly k ones picked up.
Key Insights
- The allowed operations create a “transport” mechanism where ones are moved via adjacent swaps toward a chosen starting position.
- Flips (which cost one move each) can create ones arbitrarily (subject to maxChanges), but note that after a flip a swap is needed to bring the new one next to aliceIndex (thus effectively costing 2 moves per flip‐created one).
- For ones that already exist in nums, the cost of “bringing” one from position i to aliceIndex is the distance |i – aliceIndex|. Choosing aliceIndex optimally (in practice, at the median location of a chosen block of ones) minimizes the sum of distances.
- One may decide to pick up some ones from the original array and “create” the remainder via flips. Thus the solution is to try all splits where i real ones are used (from a contiguous block) and k–i ones come from flips (at cost 2 each).
- To quickly compute the total “swap cost” for any contiguous block of ones (by aligning them around its median), a prefix sum over the indices of ones is used.
Space and Time Complexity
Time Complexity: O(n) if we precompute the positions and prefix sums and then scan through all possible contiguous blocks of real ones. Space Complexity: O(n) to store the positions of ones and the prefix-sum array.
Solution
The idea is to first collect the indices of the ones already in nums into an array (call it ones). Note that by performing at most maxChanges flips we can produce a total of (original ones count + maxChanges) ones, which is guaranteed to be at least k. Then, we consider splitting the required k ones into two groups:
- i ones taken from the original ones (for which we must “move” them to the chosen starting point using adjacent swaps).
- k – i ones that we create via flips; each such one will cost 2 moves (one for the flip and one for swapping it to aliceIndex).
For a contiguous block of i ones taken from the array ones, the best strategy is to choose aliceIndex equal to the median of those positions, because this minimizes the total swap cost, which is the sum of the absolute differences between each one’s position and the median. This sum can be computed in O(1) per block by precomputing a prefix sum of the ones array. We do this for each possible contiguous block of i ones (for all valid i) and take the minimum overall cost. Also, we consider the possibility of using only flips (i.e. i == 0) if k is not larger than maxChanges. Finally, we output the minimum moves found.
A few gotchas: • Be cautious with the adjustment for a block’s median: for a block starting at index s with length i, let m = s + i//2 be its median. Then, using the prefix sum array, the cost is computed as: cost = (ones[m](m – s) – (prefix[m–1] – (prefix[s–1] if s > 0 else 0))) + ((prefix[s+i–1] – prefix[m]) – ones[m]((s+i–1) – m)) • Remember that the one at aliceIndex (i.e. the median) is “picked up” without any swap cost. • When creating ones by flips, note that even though a flip costs 1 move, an extra swap move is necessary to move the newly created one next to aliceIndex, totalling 2 moves per flip‐created one.
Code Solutions
Below are code solutions in Python, JavaScript, C++, and Java. Each solution uses clear variable names and line‐by‐line comments.