Problem Description
Given a binary integer array and an integer k, determine the minimum number of adjacent swaps required so that the array has k consecutive 1's. In one move you can swap any two adjacent elements. The challenge is to group k ones together with the fewest total moves.
Key Insights
- Only the positions (indices) of the 1's matter; zeros are irrelevant except as gaps.
- By gathering all indices where nums[i] == 1, the problem reduces to aligning a subarray of these positions.
- The cost (number of swaps) to group a set of ones is equivalent to the sum of absolute differences between these indices and a chosen target which minimizes that sum. The median minimizes sum of absolute differences.
- A transformation (computing positions[i] - i) allows us to remove the effect of the ones being spread out in the original array. Then, a sliding window of size k with prefix sum can quickly compute the cost with the median as the anchor.
- For windows of even length, a small correction is required since the median is not unique.
Space and Time Complexity
Time Complexity: O(n) where n is the length of the input array (processing ones and then sliding over them).
Space Complexity: O(n) for storing the positions of the 1's and the prefix sum array.
Solution
The solution uses the following steps:
- Iterate through the input array to record the indices of all 1's. Let this list be positions.
- Transform the positions by subtracting the index in the positions list from each element (i.e. new_position = positions[i] - i). This step normalizes the distances so that moving a 1 over a gap is directly quantifiable.
- Compute a prefix sum array on the transformed positions.
- Use a sliding window of size k over the transformed positions. For each window:
- Identify the median element (for window indices i to i+k-1, the median is at index i+k//2).
- Compute the cost to shift all items in the window so that they become consecutive. This is done by calculating the total cost for elements on the left of the median and the right of the median using the prefix sum array.
- For even k, adjust the computed cost by subtracting the appropriate offset caused by the discrete nature of indices.
- Track and return the minimum computed cost.
The primary data structures used are an array for recording positions and another for prefix sums. The algorithmic approach includes a sliding window and careful cost computation using medians (which minimize absolute deviations).