Skip to content

Commit f8339f6

Browse files
authored
feat: add solutions to lc problem: No.2134 (#2562)
No.2134.Minimum Swaps to Group All 1's Together II
1 parent 23b0878 commit f8339f6

File tree

9 files changed

+251
-167
lines changed

9 files changed

+251
-167
lines changed

solution/2100-2199/2134.Minimum Swaps to Group All 1's Together II/README.md

+88-56
Original file line numberDiff line numberDiff line change
@@ -57,46 +57,44 @@
5757

5858
## 解法
5959

60-
### 方法一
60+
### 方法一:滑动窗口
61+
62+
我们先统计数组中 $1$ 的个数,记为 $k$,那么题目实际上就是求一个长度为 $k$ 的环形子数组,使得子数组中 $1$ 的个数最多,那么最少交换次数就是 $k$ 减去子数组中 $1$ 的个数最多的那个子数组中 $1$ 的个数。
63+
64+
我们可以使用滑动窗口来解决这个问题,首先统计数组中前 $k$ 个元素中 $1$ 的个数,记为 $cnt$,然后我们维护一个长度为 $k$ 的滑动窗口,每次向右移动一个位置,更新 $cnt$,同时更新最大的 $cnt$ 值,即 $mx = \max(mx, cnt)$,最后答案就是 $k - mx$。
65+
66+
时间复杂度 $O(n)$,其中 $n$ 是数组 $nums$ 的长度。空间复杂度 $O(1)$。
6167

6268
<!-- tabs:start -->
6369

6470
```python
6571
class Solution:
6672
def minSwaps(self, nums: List[int]) -> int:
67-
cnt = nums.count(1)
73+
k = nums.count(1)
74+
mx = cnt = sum(nums[:k])
6875
n = len(nums)
69-
s = [0] * ((n << 1) + 1)
70-
for i in range(n << 1):
71-
s[i + 1] = s[i] + nums[i % n]
72-
mx = 0
73-
for i in range(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 in range(k, n + k):
77+
cnt += nums[i % n]
78+
cnt -= nums[(i - k + n) % n]
79+
mx = max(mx, cnt)
80+
return k - mx
7881
```
7982

8083
```java
8184
class Solution {
8285
public int minSwaps(int[] nums) {
83-
int cnt = 0;
84-
for (int v : nums) {
85-
cnt += v;
86-
}
86+
int k = Arrays.stream(nums).sum();
8787
int n = nums.length;
88-
int[] s = new int[(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];
9191
}
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);
9896
}
99-
return cnt - mx;
97+
return k - mx;
10098
}
10199
}
102100
```
@@ -105,56 +103,90 @@ class Solution {
105103
class Solution {
106104
public:
107105
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);
110107
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);
117113
}
118-
return cnt - mx;
114+
return k - mx;
119115
}
120116
};
121117
```
122118
123119
```go
124120
func minSwaps(nums []int) int {
121+
k := 0
122+
for _, x := range nums {
123+
k += x
124+
}
125125
cnt := 0
126-
for _, v := range nums {
127-
cnt += v
126+
for i := 0; i < k; i++ {
127+
cnt += nums[i]
128128
}
129+
mx := cnt
129130
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)
140134
}
141-
return cnt - mx
135+
return k - mx
142136
}
143137
```
144138

145139
```ts
146140
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;
147144
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);
150-
let ans = cnt;
151-
for (let i = m; i < m + n; i++) {
152-
let prev = nums[i - m];
153-
let post = nums[i % n];
154-
cnt += post - prev;
155-
ans = Math.max(cnt, ans);
145+
for (let i = k; i < n + k; ++i) {
146+
cnt += nums[i % n] - nums[(i - k + n) % n];
147+
mx = Math.max(mx, cnt);
148+
}
149+
return k - mx;
150+
}
151+
```
152+
153+
```rust
154+
impl Solution {
155+
pub fn min_swaps(nums: Vec<i32>) -> i32 {
156+
let k: i32 = nums.iter().sum();
157+
let n: usize = nums.len();
158+
let mut cnt: i32 = 0;
159+
for i in 0..k {
160+
cnt += nums[i as usize];
161+
}
162+
let mut mx: i32 = cnt;
163+
for i in k..(n as i32) + k {
164+
cnt +=
165+
nums[(i % (n as i32)) as usize] -
166+
nums[((i - k + (n as i32)) % (n as i32)) as usize];
167+
mx = mx.max(cnt);
168+
}
169+
return k - mx;
170+
}
171+
}
172+
```
173+
174+
```cs
175+
public class Solution {
176+
public int MinSwaps(int[] nums) {
177+
int k = nums.Sum();
178+
int n = nums.Length;
179+
int cnt = 0;
180+
for (int i = 0; i < k; ++i) {
181+
cnt += nums[i];
182+
}
183+
int mx = cnt;
184+
for (int i = k; i < n + k; ++i) {
185+
cnt += nums[i % n] - nums[(i - k + n) % n];
186+
mx = Math.Max(mx, cnt);
187+
}
188+
return k - mx;
156189
}
157-
return m - ans;
158190
}
159191
```
160192

solution/2100-2199/2134.Minimum Swaps to Group All 1's Together II/README_EN.md

+88-56
Original file line numberDiff line numberDiff line change
@@ -57,46 +57,44 @@ Thus, the minimum number of swaps required is 0.
5757

5858
## Solutions
5959

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)$.
6167

6268
<!-- tabs:start -->
6369

6470
```python
6571
class Solution:
6672
def minSwaps(self, nums: List[int]) -> int:
67-
cnt = nums.count(1)
73+
k = nums.count(1)
74+
mx = cnt = sum(nums[:k])
6875
n = len(nums)
69-
s = [0] * ((n << 1) + 1)
70-
for i in range(n << 1):
71-
s[i + 1] = s[i] + nums[i % n]
72-
mx = 0
73-
for i in range(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 in range(k, n + k):
77+
cnt += nums[i % n]
78+
cnt -= nums[(i - k + n) % n]
79+
mx = max(mx, cnt)
80+
return k - mx
7881
```
7982

8083
```java
8184
class Solution {
8285
public int minSwaps(int[] nums) {
83-
int cnt = 0;
84-
for (int v : nums) {
85-
cnt += v;
86-
}
86+
int k = Arrays.stream(nums).sum();
8787
int n = nums.length;
88-
int[] s = new int[(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];
9191
}
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);
9896
}
99-
return cnt - mx;
97+
return k - mx;
10098
}
10199
}
102100
```
@@ -105,56 +103,90 @@ class Solution {
105103
class Solution {
106104
public:
107105
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);
110107
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);
117113
}
118-
return cnt - mx;
114+
return k - mx;
119115
}
120116
};
121117
```
122118
123119
```go
124120
func minSwaps(nums []int) int {
121+
k := 0
122+
for _, x := range nums {
123+
k += x
124+
}
125125
cnt := 0
126-
for _, v := range nums {
127-
cnt += v
126+
for i := 0; i < k; i++ {
127+
cnt += nums[i]
128128
}
129+
mx := cnt
129130
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)
140134
}
141-
return cnt - mx
135+
return k - mx
142136
}
143137
```
144138

145139
```ts
146140
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;
147144
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);
150-
let ans = cnt;
151-
for (let i = m; i < m + n; i++) {
152-
let prev = nums[i - m];
153-
let post = nums[i % n];
154-
cnt += post - prev;
155-
ans = Math.max(cnt, ans);
145+
for (let i = k; i < n + k; ++i) {
146+
cnt += nums[i % n] - nums[(i - k + n) % n];
147+
mx = Math.max(mx, cnt);
148+
}
149+
return k - mx;
150+
}
151+
```
152+
153+
```rust
154+
impl Solution {
155+
pub fn min_swaps(nums: Vec<i32>) -> i32 {
156+
let k: i32 = nums.iter().sum();
157+
let n: usize = nums.len();
158+
let mut cnt: i32 = 0;
159+
for i in 0..k {
160+
cnt += nums[i as usize];
161+
}
162+
let mut mx: i32 = cnt;
163+
for i in k..(n as i32) + k {
164+
cnt +=
165+
nums[(i % (n as i32)) as usize] -
166+
nums[((i - k + (n as i32)) % (n as i32)) as usize];
167+
mx = mx.max(cnt);
168+
}
169+
return k - mx;
170+
}
171+
}
172+
```
173+
174+
```cs
175+
public class Solution {
176+
public int MinSwaps(int[] nums) {
177+
int k = nums.Sum();
178+
int n = nums.Length;
179+
int cnt = 0;
180+
for (int i = 0; i < k; ++i) {
181+
cnt += nums[i];
182+
}
183+
int mx = cnt;
184+
for (int i = k; i < n + k; ++i) {
185+
cnt += nums[i % n] - nums[(i - k + n) % n];
186+
mx = Math.Max(mx, cnt);
187+
}
188+
return k - mx;
156189
}
157-
return m - ans;
158190
}
159191
```
160192

0 commit comments

Comments
 (0)