2134. Minimum Swaps to Group All 1's Together II
Description
A swap is defined as taking two distinct positions in an array and swapping the values in them.
A circular array is defined as an array where we consider the first element and the last element to be adjacent.
Given a binary circular array nums, return the minimum number of swaps required to group all 1's present in the array together at any location.
Example 1:
Input: nums = [0,1,0,1,1,0,0] Output: 1 Explanation: Here are a few of the ways to group all the 1's together: [0,0,1,1,1,0,0] using 1 swap. [0,1,1,1,0,0,0] using 1 swap. [1,1,0,0,0,0,1] using 2 swaps (using the circular property of the array). There is no way to group all 1's together with 0 swaps. Thus, the minimum number of swaps required is 1.
Example 2:
Input: nums = [0,1,1,1,0,0,1,1,0] Output: 2 Explanation: Here are a few of the ways to group all the 1's together: [1,1,1,0,0,0,0,1,1] using 2 swaps (using the circular property of the array). [1,1,1,1,1,0,0,0,0] using 2 swaps. There is no way to group all 1's together with 0 or 1 swaps. Thus, the minimum number of swaps required is 2.
Example 3:
Input: nums = [1,1,0,0,1] Output: 0 Explanation: All the 1's are already grouped together due to the circular property of the array. Thus, the minimum number of swaps required is 0.
Constraints:
1 <= nums.length <= 105nums[i]is either0or1.
Solutions
Solution 1: Sliding Window
Thinking
Grouping all \(1\)s on a circle is equivalent to a window of length \(k=\textit{count}(1)\) that contains as many \(1\)s as possible; the answer is \(k\) minus that maximum. Recounting every window is quadratic.
A fixed-length circular window is a modular slide of \(k\) extra steps. Count \(1\)s in the first \(k\) cells, then add the entering index and drop the leaving one.
After a full pass return \(k-\textit{mx}\).
First, we count the number of \(1\)s in the array, denoted as \(k\). The problem is actually asking for a circular subarray of length \(k\) that contains the maximum number of \(1\)s. Therefore, the minimum number of swaps is \(k\) minus the maximum number of \(1\)s in that subarray.
We can solve this problem using a sliding window. First, we count the number of \(1\)s in the first \(k\) elements of the array, denoted as \(cnt\). Then, we maintain a sliding window of length \(k\). Each time we move the window one position to the right, we update \(cnt\) and simultaneously update the maximum \(cnt\) value, i.e., \(mx = \max(mx, cnt)\). Finally, the answer is \(k - mx\).
The time complexity is \(O(n)\), where \(n\) is the length of the array \(nums\). The space complexity is \(O(1)\).
1 2 3 4 5 6 7 8 9 10 | |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | |
1 2 3 4 5 6 7 8 9 10 11 12 13 | |
1 2 3 4 5 6 7 8 9 10 11 12 13 | |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | |
Solution 2: Prefix Sum
Thinking
Solution 1 wraps indices with modulo. Prefix sums on the linear array also count a value \(x\) in every window whose length equals the global number of \(x\); swaps equal that length minus the window count.
Gathering all \(1\)s on a circle is the complement view of gathering all \(0\)s, so the implementation takes the minimum of the two prefix-sum answers.
This records the prefix-sum formulation of the same window problem.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | |