Skip to content

Commit 3348f0e

Browse files
committed
feat: add solutions to lc problem: No.1703
No.1703.Minimum Adjacent Swaps for K Consecutive Ones
1 parent 2e03b32 commit 3348f0e

File tree

6 files changed

+337
-2
lines changed

6 files changed

+337
-2
lines changed

solution/1700-1799/1703.Minimum Adjacent Swaps for K Consecutive Ones/README.md

Lines changed: 122 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,22 +47,143 @@
4747

4848
<!-- 这里可写通用的实现逻辑 -->
4949

50+
**方法一:前缀和 + 中位数枚举**
51+
52+
我们可以将数组 `nums` 中的 `1` 的下标存入数组 `arr` 中。接下来,我们预处理数组 `arr` 的前缀和数组 `s`,其中 `s[i]` 表示数组 `arr` 中前 $i$ 个元素的和。
53+
54+
对于长度为 $k$ 的子数组,左侧(包含中位数)的元素个数 $x=\frac{k+1}{2}$,右侧的元素个数为 $y=k-x$。
55+
56+
我们枚举中位数的下标 $i$,其中 $x-1\leq i\leq len(arr)-y$,那么左侧数组的前缀和 $ls=s[i+1]-s[i+1-x]$,右侧数组的前缀和 $rs=s[i+1+y]-s[i+1]$。当前中位数在 `nums` 中的下标为 $j=arr[i]$,将左侧 $x$ 个元素移动到 $[j-x+1,..j]$ 所需要的操作次数为 $a=(j+j-x+1)\times\frac{x}{2}-ls$,将右侧 $y$ 个元素移动到 $[j+1,..j+y]$ 所需要的操作次数为 $b=rs-(j+1+j+y)\times\frac{y}{2}$,那么总的操作次数为 $a+b$,我们取所有总的操作次数的最小值即可。
57+
58+
时间复杂度 $O(n)$,空间复杂度 $O(m)$。其中 $n$ 和 $m$ 分别为数组 `nums` 的长度以及数组 `nums``1` 的个数。
59+
5060
<!-- tabs:start -->
5161

5262
### **Python3**
5363

5464
<!-- 这里可写当前语言的特殊实现逻辑 -->
5565

5666
```python
57-
67+
class Solution:
68+
def minMoves(self, nums: List[int], k: int) -> int:
69+
arr = [i for i, x in enumerate(nums) if x]
70+
s = list(accumulate(arr, initial=0))
71+
ans = inf
72+
x = (k + 1) // 2
73+
y = k - x
74+
for i in range(x - 1, len(arr) - y):
75+
j = arr[i]
76+
ls = s[i + 1] - s[i + 1 - x]
77+
rs = s[i + 1 + y] - s[i + 1]
78+
a = (j + j - x + 1) * x // 2 - ls
79+
b = rs - (j + 1 + j + y) * y // 2
80+
ans = min(ans, a + b)
81+
return ans
5882
```
5983

6084
### **Java**
6185

6286
<!-- 这里可写当前语言的特殊实现逻辑 -->
6387

6488
```java
89+
class Solution {
90+
public int minMoves(int[] nums, int k) {
91+
List<Integer> arr = new ArrayList<>();
92+
int n = nums.length;
93+
for (int i = 0; i < n; ++i) {
94+
if (nums[i] != 0) {
95+
arr.add(i);
96+
}
97+
}
98+
int m = arr.size();
99+
int[] s = new int[m + 1];
100+
for (int i = 0; i < m; ++i) {
101+
s[i + 1] = s[i] + arr.get(i);
102+
}
103+
long ans = 1 << 60;
104+
int x = (k + 1) / 2;
105+
int y = k - x;
106+
for (int i = x - 1; i < m - y; ++i) {
107+
int j = arr.get(i);
108+
int ls = s[i + 1] - s[i + 1 - x];
109+
int rs = s[i + 1 + y] - s[i + 1];
110+
long a = (j + j - x + 1L) * x / 2 - ls;
111+
long b = rs - (j + 1L + j + y) * y / 2;
112+
ans = Math.min(ans, a + b);
113+
}
114+
return (int) ans;
115+
}
116+
}
117+
```
118+
119+
### **C++**
120+
121+
```cpp
122+
class Solution {
123+
public:
124+
int minMoves(vector<int>& nums, int k) {
125+
vector<int> arr;
126+
for (int i = 0; i < nums.size(); ++i) {
127+
if (nums[i]) {
128+
arr.push_back(i);
129+
}
130+
}
131+
int m = arr.size();
132+
long s[m + 1];
133+
s[0] = 1;
134+
for (int i = 0; i < m; ++i) {
135+
s[i + 1] = s[i] + arr[i];
136+
}
137+
long ans = 1L << 60;
138+
int x = (k + 1) / 2;
139+
int y = k - x;
140+
for (int i = x - 1; i < m - y; ++i) {
141+
int j = arr[i];
142+
int ls = s[i + 1] - s[i + 1 - x];
143+
int rs = s[i + 1 + y] - s[i + 1];
144+
long a = (j + j - x + 1L) * x / 2 - ls;
145+
long b = rs - (j + 1L + j + y) * y / 2;
146+
ans = min(ans, a + b);
147+
}
148+
return ans;
149+
}
150+
};
151+
```
65152
153+
### **Go**
154+
155+
```go
156+
func minMoves(nums []int, k int) int {
157+
arr := []int{}
158+
for i, x := range nums {
159+
if x != 0 {
160+
arr = append(arr, i)
161+
}
162+
}
163+
s := make([]int, len(arr)+1)
164+
for i, x := range arr {
165+
s[i+1] = s[i] + x
166+
}
167+
ans := 1 << 60
168+
x := (k + 1) / 2
169+
y := k - x
170+
for i := x - 1; i < len(arr)-y; i++ {
171+
j := arr[i]
172+
ls := s[i+1] - s[i+1-x]
173+
rs := s[i+1+y] - s[i+1]
174+
a := (j+j-x+1)*x/2 - ls
175+
b := rs - (j+1+j+y)*y/2
176+
ans = min(ans, a+b)
177+
}
178+
return ans
179+
}
180+
181+
func min(a, b int) int {
182+
if a < b {
183+
return a
184+
}
185+
return b
186+
}
66187
```
67188

68189
### **...**

solution/1700-1799/1703.Minimum Adjacent Swaps for K Consecutive Ones/README_EN.md

Lines changed: 112 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,13 +49,124 @@
4949
### **Python3**
5050

5151
```python
52-
52+
class Solution:
53+
def minMoves(self, nums: List[int], k: int) -> int:
54+
arr = [i for i, x in enumerate(nums) if x]
55+
s = list(accumulate(arr, initial=0))
56+
ans = inf
57+
x = (k + 1) // 2
58+
y = k - x
59+
for i in range(x - 1, len(arr) - y):
60+
j = arr[i]
61+
ls = s[i + 1] - s[i + 1 - x]
62+
rs = s[i + 1 + y] - s[i + 1]
63+
a = (j + j - x + 1) * x // 2 - ls
64+
b = rs - (j + 1 + j + y) * y // 2
65+
ans = min(ans, a + b)
66+
return ans
5367
```
5468

5569
### **Java**
5670

5771
```java
72+
class Solution {
73+
public int minMoves(int[] nums, int k) {
74+
List<Integer> arr = new ArrayList<>();
75+
int n = nums.length;
76+
for (int i = 0; i < n; ++i) {
77+
if (nums[i] != 0) {
78+
arr.add(i);
79+
}
80+
}
81+
int m = arr.size();
82+
int[] s = new int[m + 1];
83+
for (int i = 0; i < m; ++i) {
84+
s[i + 1] = s[i] + arr.get(i);
85+
}
86+
long ans = 1 << 60;
87+
int x = (k + 1) / 2;
88+
int y = k - x;
89+
for (int i = x - 1; i < m - y; ++i) {
90+
int j = arr.get(i);
91+
int ls = s[i + 1] - s[i + 1 - x];
92+
int rs = s[i + 1 + y] - s[i + 1];
93+
long a = (j + j - x + 1L) * x / 2 - ls;
94+
long b = rs - (j + 1L + j + y) * y / 2;
95+
ans = Math.min(ans, a + b);
96+
}
97+
return (int) ans;
98+
}
99+
}
100+
```
101+
102+
### **C++**
103+
104+
```cpp
105+
class Solution {
106+
public:
107+
int minMoves(vector<int>& nums, int k) {
108+
vector<int> arr;
109+
for (int i = 0; i < nums.size(); ++i) {
110+
if (nums[i]) {
111+
arr.push_back(i);
112+
}
113+
}
114+
int m = arr.size();
115+
long s[m + 1];
116+
s[0] = 1;
117+
for (int i = 0; i < m; ++i) {
118+
s[i + 1] = s[i] + arr[i];
119+
}
120+
long ans = 1L << 60;
121+
int x = (k + 1) / 2;
122+
int y = k - x;
123+
for (int i = x - 1; i < m - y; ++i) {
124+
int j = arr[i];
125+
int ls = s[i + 1] - s[i + 1 - x];
126+
int rs = s[i + 1 + y] - s[i + 1];
127+
long a = (j + j - x + 1L) * x / 2 - ls;
128+
long b = rs - (j + 1L + j + y) * y / 2;
129+
ans = min(ans, a + b);
130+
}
131+
return ans;
132+
}
133+
};
134+
```
58135
136+
### **Go**
137+
138+
```go
139+
func minMoves(nums []int, k int) int {
140+
arr := []int{}
141+
for i, x := range nums {
142+
if x != 0 {
143+
arr = append(arr, i)
144+
}
145+
}
146+
s := make([]int, len(arr)+1)
147+
for i, x := range arr {
148+
s[i+1] = s[i] + x
149+
}
150+
ans := 1 << 60
151+
x := (k + 1) / 2
152+
y := k - x
153+
for i := x - 1; i < len(arr)-y; i++ {
154+
j := arr[i]
155+
ls := s[i+1] - s[i+1-x]
156+
rs := s[i+1+y] - s[i+1]
157+
a := (j+j-x+1)*x/2 - ls
158+
b := rs - (j+1+j+y)*y/2
159+
ans = min(ans, a+b)
160+
}
161+
return ans
162+
}
163+
164+
func min(a, b int) int {
165+
if a < b {
166+
return a
167+
}
168+
return b
169+
}
59170
```
60171

61172
### **...**
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
class Solution {
2+
public:
3+
int minMoves(vector<int>& nums, int k) {
4+
vector<int> arr;
5+
for (int i = 0; i < nums.size(); ++i) {
6+
if (nums[i]) {
7+
arr.push_back(i);
8+
}
9+
}
10+
int m = arr.size();
11+
long s[m + 1];
12+
s[0] = 1;
13+
for (int i = 0; i < m; ++i) {
14+
s[i + 1] = s[i] + arr[i];
15+
}
16+
long ans = 1L << 60;
17+
int x = (k + 1) / 2;
18+
int y = k - x;
19+
for (int i = x - 1; i < m - y; ++i) {
20+
int j = arr[i];
21+
int ls = s[i + 1] - s[i + 1 - x];
22+
int rs = s[i + 1 + y] - s[i + 1];
23+
long a = (j + j - x + 1L) * x / 2 - ls;
24+
long b = rs - (j + 1L + j + y) * y / 2;
25+
ans = min(ans, a + b);
26+
}
27+
return ans;
28+
}
29+
};
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
func minMoves(nums []int, k int) int {
2+
arr := []int{}
3+
for i, x := range nums {
4+
if x != 0 {
5+
arr = append(arr, i)
6+
}
7+
}
8+
s := make([]int, len(arr)+1)
9+
for i, x := range arr {
10+
s[i+1] = s[i] + x
11+
}
12+
ans := 1 << 60
13+
x := (k + 1) / 2
14+
y := k - x
15+
for i := x - 1; i < len(arr)-y; i++ {
16+
j := arr[i]
17+
ls := s[i+1] - s[i+1-x]
18+
rs := s[i+1+y] - s[i+1]
19+
a := (j+j-x+1)*x/2 - ls
20+
b := rs - (j+1+j+y)*y/2
21+
ans = min(ans, a+b)
22+
}
23+
return ans
24+
}
25+
26+
func min(a, b int) int {
27+
if a < b {
28+
return a
29+
}
30+
return b
31+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
class Solution {
2+
public int minMoves(int[] nums, int k) {
3+
List<Integer> arr = new ArrayList<>();
4+
int n = nums.length;
5+
for (int i = 0; i < n; ++i) {
6+
if (nums[i] != 0) {
7+
arr.add(i);
8+
}
9+
}
10+
int m = arr.size();
11+
int[] s = new int[m + 1];
12+
for (int i = 0; i < m; ++i) {
13+
s[i + 1] = s[i] + arr.get(i);
14+
}
15+
long ans = 1 << 60;
16+
int x = (k + 1) / 2;
17+
int y = k - x;
18+
for (int i = x - 1; i < m - y; ++i) {
19+
int j = arr.get(i);
20+
int ls = s[i + 1] - s[i + 1 - x];
21+
int rs = s[i + 1 + y] - s[i + 1];
22+
long a = (j + j - x + 1L) * x / 2 - ls;
23+
long b = rs - (j + 1L + j + y) * y / 2;
24+
ans = Math.min(ans, a + b);
25+
}
26+
return (int) ans;
27+
}
28+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
class Solution:
2+
def minMoves(self, nums: List[int], k: int) -> int:
3+
arr = [i for i, x in enumerate(nums) if x]
4+
s = list(accumulate(arr, initial=0))
5+
ans = inf
6+
x = (k + 1) // 2
7+
y = k - x
8+
for i in range(x - 1, len(arr) - y):
9+
j = arr[i]
10+
ls = s[i + 1] - s[i + 1 - x]
11+
rs = s[i + 1 + y] - s[i + 1]
12+
a = (j + j - x + 1) * x // 2 - ls
13+
b = rs - (j + 1 + j + y) * y // 2
14+
ans = min(ans, a + b)
15+
return ans

0 commit comments

Comments
 (0)