[LeetCode] 2134. Minimum Swaps to Group All 1s Together II

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 <= 105
nums[i] is either 0 or 1.

最少交换次数来组合所有的 1 II。

交换 定义为选中一个数组中的两个 互不相同 的位置并交换二者的值。 环形 数组是一个数组,可以认为 第一个 元素和 最后一个 元素 相邻 。 给你一个 二进制环形 数组 nums ,返回在 任意位置 将数组中的所有 1 聚集在一起需要的最少交换次数。

思路

这道题跟版本一 1151 题很像,唯一不同的地方是题目多了一个条件,给的 input 是环形数组。环形数组中找连续的 1 的个数其实是比较难的,因为最优解有可能是断开的(一部分 1 在数组起点,一部分 1 在数组终点),但是我们可以换一种思路。因为 input 数组中只有 0 和 1 两种数字,那么我们可以试着比较把所有的 1 聚集在一起的开销少还是把所有的 0 聚集在一起的开销少。

复杂度

时间O(n)
空间O(1)

代码

Java实现

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
class Solution {
public int minSwaps(int[] nums) {
int n = nums.length;
int ones = 0;
int zeros = 0;
for (int num : nums) {
if (num == 0) {
zeros++;
}
}
ones = n - zeros;

int a = helper(nums, 0, zeros);
int b = helper(nums, 1, ones);
return Math.min(a, b);
}

private int helper(int[] nums, int target, int p) {
int start = 0;
int end = 0;
int count = 0;
int res = Integer.MAX_VALUE;
while (end < nums.length) {
if (nums[end] == target) {
count++;
}
end++;
if (end - start == p) {
res = Math.min(res, p - count);
if (nums[start] == target) {
count--;
}
start++;
}
}
return res;
}
}

相关题目

1
2
1151. Minimum Swaps to Group All 1s Together
2134. Minimum Swaps to Group All 1s Together II

[LeetCode] 2134. Minimum Swaps to Group All 1s Together II
https://shurui91.github.io/posts/289974058.html
Author
Aaron Liu
Posted on
August 3, 2024
Licensed under