You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardexpand all lines: solution/2100-2199/2134.Minimum Swaps to Group All 1's Together II/README_EN.md
+88-56
Original file line number
Diff line number
Diff line change
@@ -57,46 +57,44 @@ Thus, the minimum number of swaps required is 0.
57
57
58
58
## Solutions
59
59
60
-
### Solution 1
60
+
### Solution 1: Sliding Window
61
+
62
+
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.
63
+
64
+
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$.
65
+
66
+
The time complexity is $O(n)$, where $n$ is the length of the array $nums$. The space complexity is $O(1)$.
61
67
62
68
<!-- tabs:start -->
63
69
64
70
```python
65
71
classSolution:
66
72
defminSwaps(self, nums: List[int]) -> int:
67
-
cnt = nums.count(1)
73
+
k = nums.count(1)
74
+
mx = cnt =sum(nums[:k])
68
75
n =len(nums)
69
-
s = [0] * ((n <<1) +1)
70
-
for i inrange(n <<1):
71
-
s[i +1] = s[i] + nums[i % n]
72
-
mx =0
73
-
for i inrange(n <<1):
74
-
j = i + cnt -1
75
-
if j < (n <<1):
76
-
mx =max(mx, s[j +1] - s[i])
77
-
return cnt - mx
76
+
for i inrange(k, n + k):
77
+
cnt += nums[i % n]
78
+
cnt -= nums[(i - k + n) % n]
79
+
mx =max(mx, cnt)
80
+
return k - mx
78
81
```
79
82
80
83
```java
81
84
classSolution {
82
85
publicintminSwaps(int[] nums) {
83
-
int cnt =0;
84
-
for (int v : nums) {
85
-
cnt += v;
86
-
}
86
+
int k =Arrays.stream(nums).sum();
87
87
int n = nums.length;
88
-
int[] s=newint[(n <<1) +1];
89
-
for (int i =0; i <(n <<1); ++i) {
90
-
s[i +1] = s[i] +nums[i% n];
88
+
int cnt=0;
89
+
for (int i =0; i <k; ++i) {
90
+
cnt +=nums[i];
91
91
}
92
-
int mx =0;
93
-
for (int i =0; i < (n <<1); ++i) {
94
-
int j = i + cnt -1;
95
-
if (j < (n <<1)) {
96
-
mx =Math.max(mx, s[j +1] - s[i]);
97
-
}
92
+
int mx = cnt;
93
+
for (int i = k; i < n + k; ++i) {
94
+
cnt += nums[i % n] - nums[(i - k + n) % n];
95
+
mx =Math.max(mx, cnt);
98
96
}
99
-
returncnt- mx;
97
+
returnk- mx;
100
98
}
101
99
}
102
100
```
@@ -105,56 +103,90 @@ class Solution {
105
103
classSolution {
106
104
public:
107
105
int minSwaps(vector<int>& nums) {
108
-
int cnt = 0;
109
-
for (int& v : nums) cnt += v;
106
+
int k = accumulate(nums.begin(), nums.end(), 0);
110
107
int n = nums.size();
111
-
vector<int> s((n << 1) + 1);
112
-
for (int i = 0; i < (n << 1); ++i) s[i + 1] = s[i] + nums[i % n];
113
-
int mx = 0;
114
-
for (int i = 0; i < (n << 1); ++i) {
115
-
int j = i + cnt - 1;
116
-
if (j < (n << 1)) mx = max(mx, s[j + 1] - s[i]);
108
+
int cnt = accumulate(nums.begin(), nums.begin() + k, 0);
109
+
int mx = cnt;
110
+
for (int i = k; i < n + k; ++i) {
111
+
cnt += nums[i % n] - nums[(i - k + n) % n];
112
+
mx = max(mx, cnt);
117
113
}
118
-
return cnt - mx;
114
+
return k - mx;
119
115
}
120
116
};
121
117
```
122
118
123
119
```go
124
120
func minSwaps(nums []int) int {
121
+
k := 0
122
+
for _, x := range nums {
123
+
k += x
124
+
}
125
125
cnt := 0
126
-
for _, v := range nums {
127
-
cnt += v
126
+
for i := 0; i < k; i++ {
127
+
cnt += nums[i]
128
128
}
129
+
mx := cnt
129
130
n := len(nums)
130
-
s := make([]int, (n<<1)+1)
131
-
for i := 0; i < (n << 1); i++ {
132
-
s[i+1] = s[i] + nums[i%n]
133
-
}
134
-
mx := 0
135
-
for i := 0; i < (n << 1); i++ {
136
-
j := i + cnt - 1
137
-
if j < (n << 1) {
138
-
mx = max(mx, s[j+1]-s[i])
139
-
}
131
+
for i := k; i < n+k; i++ {
132
+
cnt += nums[i%n] - nums[(i-k+n)%n]
133
+
mx = max(mx, cnt)
140
134
}
141
-
return cnt - mx
135
+
return k - mx
142
136
}
143
137
```
144
138
145
139
```ts
146
140
function minSwaps(nums:number[]):number {
141
+
const k =nums.reduce((a, b) =>a+b, 0);
142
+
let cnt =nums.slice(0, k).reduce((a, b) =>a+b, 0);
143
+
let mx =cnt;
147
144
const n =nums.length;
148
-
const m =nums.reduce((a, c) =>a+c, 0);
149
-
let cnt =nums.reduce((a, c, i) =>a+ (i<m?c:0), 0);
0 commit comments