forked from fishercoder1534/Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path_2134.java
44 lines (42 loc) · 1.35 KB
/
_2134.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
39
40
41
42
43
44
package com.fishercoder.solutions;
import java.util.ArrayList;
import java.util.List;
public class _2134 {
public static class Solution1 {
/**
* Connect the original array with itself to simulate the circular property of this array.
* Then use a sliding window to find the minimum swaps.
*/
public int minSwaps(int[] nums) {
int ones = 0;
List<Integer> list = new ArrayList<>();
for (int num : nums) {
ones += num;
list.add(num);
}
for (int num : nums) {
list.add(num);
}
int minSwaps = nums.length;
int zeroes = 0;
for (int left = 0, right = 0; right < list.size(); right++) {
if (list.get(right) == 0) {
zeroes++;
}
int len = right - left + 1;
if (len < ones) {
continue;
} else if (len == ones) {
minSwaps = Math.min(minSwaps, zeroes);
} else {
if (list.get(left) == 0) {
zeroes--;
}
left++;
minSwaps = Math.min(minSwaps, zeroes);
}
}
return minSwaps;
}
}
}