diff --git a/solution/1600-1699/1674.Minimum Moves to Make Array Complementary/README.md b/solution/1600-1699/1674.Minimum Moves to Make Array Complementary/README.md
index 1e02b6deb8250..53f1b99877669 100644
--- a/solution/1600-1699/1674.Minimum Moves to Make Array Complementary/README.md
+++ b/solution/1600-1699/1674.Minimum Moves to Make Array Complementary/README.md
@@ -76,27 +76,33 @@ nums[3] + nums[0] = 3 + 1 = 4.
### 方法一:差分数组
-我们不妨设 $a$ 为 $nums[i]$ 和 $nums[n-i-1]$ 的较小值,设 $b$ 为 $nums[i]$ 和 $nums[n-i-1]$ 的较大值。
+假设最终的数组中,数对 $\textit{nums}[i]$ 和 $\textit{nums}[n-i-1]$ 的和为 $s$。
-假设经过替换后,两数之和为 $x$。由题意,我们知道 $x$ 最小值为 $2$,即两个数替换为 $1$;最大值为 $2 \times limit$,即两个数都替换为 $limit$。因此 $x$ 的取值范围是 $[2,... 2 \times limit]$。
+我们不妨设 $x$ 为 $\textit{nums}[i]$ 和 $\textit{nums}[n-i-1]$ 的较小值,设 $y$ 为 $\textit{nums}[i]$ 和 $\textit{nums}[n-i-1]$ 的较大值。
-如何求出对于不同的 $x$,需要替换的最少次数呢?
+对于每一对数,我们有以下几种情况:
-我们分析发现:
+- 如果不需要替换,那么 $x + y = s$。
+- 如果替换一次,那么 $x + 1 \le s \le y + \textit{limit}$。
+- 如果替换两次,那么 $2 \le s \le x$ 或 $y + \textit{limit} + 1 \le s \le 2 \times \textit{limit}$。
-- 如果 $x = a + b$,那么我们需要替换的次数为 $0$,即当前的数对已经满足互补的要求;
-- 否则如果 $1 + a \le x \le limit + b $,那么我们需要替换的次数为 $1$,即把其中一个数替换即可;
-- 否则如果 $2 \le x \le 2 \times limit$,那么我们需要替换的次数为 $2$,即把两个数都替换。
+即:
-因此,我们可以遍历每一对数,执行如下操作:
+- 在 $[2,..x]$ 范围内,需要替换 $2$ 次。
+- 在 $[x+1,..x+y-1]$ 范围内,需要替换 $1$ 次。
+- 在 $[x+y]$ 时,不需要替换。
+- 在 $[x+y+1,..y + \textit{limit}]$ 范围内,需要替换 $1$ 次。
+- 在 $[y + \textit{limit} + 1,..2 \times \textit{limit}]$ 范围内,需要替换 $2$ 次。
-1. 先将 $[2,... 2 \times limit]$ 范围需要的操作次数加 $2$。
-1. 再将 $[1 + a,... limit + b]$ 范围需要的操作次数减 $1$。
-1. 最后将 $[a + b,... a + b]$ 范围需要的操作次数减 $1$。
+我们枚举每一个数对,利用差分数组,更新每个数对在不同区间范围内的替换次数。
-可以发现,这实际上是在对一个连续区间内的元素进行加减操作,因此我们可以使用差分数组来实现。
+最后,我们求出下标 $2$ 到 $2 \times \textit{limit}$ 的前缀和中的最小值,即为最少的替换次数。
-时间复杂度 $O(n)$,空间复杂度 $O(n)$。其中 $n$ 为数组 $nums$ 的长度。
+时间复杂度 $O(n)$,空间复杂度 $O(n)$。其中 $n$ 为数组 $\textit{nums}$ 的长度。
+
+相似题目:
+
+- [3224. 使差值相等的最少数组改动次数](https://github.com/doocs/leetcode/blob/main/solution/3200-3299/3224.Minimum%20Array%20Changes%20to%20Make%20Differences%20Equal/README.md)
@@ -105,27 +111,20 @@ nums[3] + nums[0] = 3 + 1 = 4.
```python
class Solution:
def minMoves(self, nums: List[int], limit: int) -> int:
- d = [0] * (limit * 2 + 2)
+ d = [0] * (2 * limit + 2)
n = len(nums)
-
- for i in range(n >> 1):
- a, b = min(nums[i], nums[n - i - 1]), max(nums[i], nums[n - i - 1])
-
+ for i in range(n // 2):
+ x, y = nums[i], nums[-i - 1]
+ if x > y:
+ x, y = y, x
d[2] += 2
- d[limit * 2 + 1] -= 2
-
- d[a + 1] -= 1
- d[b + limit + 1] += 1
-
- d[a + b] -= 1
- d[a + b + 1] += 1
-
- ans, s = n, 0
- for v in d[2 : limit * 2 + 1]:
- s += v
- if ans > s:
- ans = s
- return ans
+ d[x + 1] -= 2
+ d[x + 1] += 1
+ d[x + y] -= 1
+ d[x + y + 1] += 1
+ d[y + limit + 1] -= 1
+ d[y + limit + 1] += 2
+ return min(accumulate(d[2:]))
```
#### Java
@@ -133,27 +132,23 @@ class Solution:
```java
class Solution {
public int minMoves(int[] nums, int limit) {
+ int[] d = new int[2 * limit + 2];
int n = nums.length;
- int[] d = new int[limit * 2 + 2];
- for (int i = 0; i < n >> 1; ++i) {
- int a = Math.min(nums[i], nums[n - i - 1]);
- int b = Math.max(nums[i], nums[n - i - 1]);
-
+ for (int i = 0; i < n / 2; ++i) {
+ int x = Math.min(nums[i], nums[n - i - 1]);
+ int y = Math.max(nums[i], nums[n - i - 1]);
d[2] += 2;
- d[limit * 2 + 1] -= 2;
-
- d[a + 1] -= 1;
- d[b + limit + 1] += 1;
-
- d[a + b] -= 1;
- d[a + b + 1] += 1;
+ d[x + 1] -= 2;
+ d[x + 1] += 1;
+ d[x + y] -= 1;
+ d[x + y + 1] += 1;
+ d[y + limit + 1] -= 1;
+ d[y + limit + 1] += 2;
}
- int ans = n, s = 0;
- for (int i = 2; i <= limit * 2; ++i) {
+ int ans = n;
+ for (int i = 2, s = 0; i < d.length; ++i) {
s += d[i];
- if (ans > s) {
- ans = s;
- }
+ ans = Math.min(ans, s);
}
return ans;
}
@@ -167,26 +162,25 @@ class Solution {
public:
int minMoves(vector& nums, int limit) {
int n = nums.size();
- vector d(limit * 2 + 2);
- for (int i = 0; i < n >> 1; ++i) {
- int a = min(nums[i], nums[n - i - 1]);
- int b = max(nums[i], nums[n - i - 1]);
-
+ int d[limit * 2 + 2];
+ memset(d, 0, sizeof(d));
+ for (int i = 0; i < n / 2; ++i) {
+ int x = nums[i], y = nums[n - i - 1];
+ if (x > y) {
+ swap(x, y);
+ }
d[2] += 2;
- d[limit * 2 + 1] -= 2;
-
- d[a + 1] -= 1;
- d[b + limit + 1] += 1;
-
- d[a + b] -= 1;
- d[a + b + 1] += 1;
+ d[x + 1] -= 2;
+ d[x + 1] += 1;
+ d[x + y] -= 1;
+ d[x + y + 1] += 1;
+ d[y + limit + 1] -= 1;
+ d[y + limit + 1] += 2;
}
- int ans = n, s = 0;
- for (int i = 2; i <= limit * 2; ++i) {
+ int ans = n;
+ for (int i = 2, s = 0; i <= limit * 2; ++i) {
s += d[i];
- if (ans > s) {
- ans = s;
- }
+ ans = min(ans, s);
}
return ans;
}
@@ -197,25 +191,25 @@ public:
```go
func minMoves(nums []int, limit int) int {
- d := make([]int, limit*2+2)
n := len(nums)
- for i := 0; i < n>>1; i++ {
- a, b := min(nums[i], nums[n-i-1]), max(nums[i], nums[n-i-1])
+ d := make([]int, 2*limit+2)
+ for i := 0; i < n/2; i++ {
+ x, y := nums[i], nums[n-1-i]
+ if x > y {
+ x, y = y, x
+ }
d[2] += 2
- d[limit*2+1] -= 2
-
- d[a+1] -= 1
- d[b+limit+1] += 1
-
- d[a+b] -= 1
- d[a+b+1] += 1
+ d[x+1] -= 2
+ d[x+1] += 1
+ d[x+y] -= 1
+ d[x+y+1] += 1
+ d[y+limit+1] -= 1
+ d[y+limit+1] += 2
}
ans, s := n, 0
- for _, v := range d[2 : limit*2+1] {
- s += v
- if ans > s {
- ans = s
- }
+ for _, x := range d[2:] {
+ s += x
+ ans = min(ans, s)
}
return ans
}
@@ -228,25 +222,21 @@ function minMoves(nums: number[], limit: number): number {
const n = nums.length;
const d: number[] = Array(limit * 2 + 2).fill(0);
for (let i = 0; i < n >> 1; ++i) {
- const a = Math.min(nums[i], nums[n - i - 1]);
- const b = Math.max(nums[i], nums[n - i - 1]);
-
+ const x = Math.min(nums[i], nums[n - 1 - i]);
+ const y = Math.max(nums[i], nums[n - 1 - i]);
d[2] += 2;
- d[limit * 2 + 1] -= 2;
-
- d[a + 1] -= 1;
- d[b + limit + 1] += 1;
-
- d[a + b] -= 1;
- d[a + b + 1] += 1;
+ d[x + 1] -= 2;
+ d[x + 1] += 1;
+ d[x + y] -= 1;
+ d[x + y + 1] += 1;
+ d[y + limit + 1] -= 1;
+ d[y + limit + 1] += 2;
}
let ans = n;
let s = 0;
- for (let i = 2; i <= limit * 2; ++i) {
+ for (let i = 2; i < d.length; ++i) {
s += d[i];
- if (ans > s) {
- ans = s;
- }
+ ans = Math.min(ans, s);
}
return ans;
}
diff --git a/solution/1600-1699/1674.Minimum Moves to Make Array Complementary/README_EN.md b/solution/1600-1699/1674.Minimum Moves to Make Array Complementary/README_EN.md
index e4758d80752a9..c269e60b80436 100644
--- a/solution/1600-1699/1674.Minimum Moves to Make Array Complementary/README_EN.md
+++ b/solution/1600-1699/1674.Minimum Moves to Make Array Complementary/README_EN.md
@@ -74,27 +74,33 @@ Therefore, nums[i] + nums[n-1-i] = 4 for every i, so nums is complementary.
### Solution 1: Difference Array
-Let's denote $a$ as the smaller value between $nums[i]$ and $nums[n-i-1]$, and $b$ as the larger value between $nums[i]$ and $nums[n-i-1]$.
+Assume that in the final array, the sum of the pair $\textit{nums}[i]$ and $\textit{nums}[n-i-1]$ is $s$.
-Suppose that after replacement, the sum of the two numbers is $x$. From the problem, we know that the minimum value of $x$ is $2$, which means both numbers are replaced by $1$; the maximum value is $2 \times limit$, which means both numbers are replaced by $limit$. Therefore, the range of $x$ is $[2,... 2 \times limit]$.
+Let's denote $x$ as the smaller value between $\textit{nums}[i]$ and $\textit{nums}[n-i-1]$, and $y$ as the larger value.
-How to find the minimum number of replacements for different $x$?
+For each pair of numbers, we have the following scenarios:
-We analyze and find:
+- If no replacement is needed, then $x + y = s$.
+- If one replacement is made, then $x + 1 \le s \le y + \textit{limit}$.
+- If two replacements are made, then $2 \le s \le x$ or $y + \textit{limit} + 1 \le s \le 2 \times \textit{limit}$.
-- If $x = a + b$, then the number of replacements we need is $0$, which means the current pair of numbers already meets the complement requirement;
-- Otherwise, if $1 + a \le x \le limit + b $, then the number of replacements we need is $1$, which means we can replace one of the numbers;
-- Otherwise, if $2 \le x \le 2 \times limit$, then the number of replacements we need is $2$, which means we need to replace both numbers.
+That is:
-Therefore, we can iterate over each pair of numbers and perform the following operations:
+- In the range $[2,..x]$, $2$ replacements are needed.
+- In the range $[x+1,..x+y-1]$, $1$ replacement is needed.
+- At $[x+y]$, no replacement is needed.
+- In the range $[x+y+1,..y + \textit{limit}]$, $1$ replacement is needed.
+- In the range $[y + \textit{limit} + 1,..2 \times \textit{limit}]$, $2$ replacements are needed.
-1. First, add $2$ to the number of operations required in the range $[2,... 2 \times limit]$.
-1. Then, subtract $1$ from the number of operations required in the range $[1 + a,... limit + b]$.
-1. Finally, subtract $1$ from the number of operations required in the range $[a + b,... a + b]$.
+We enumerate each pair of numbers and use a difference array to update the number of replacements needed in different ranges for each pair.
-We can see that this is actually adding and subtracting elements in a continuous interval, so we can use a difference array to implement it.
+Finally, we find the minimum value among the prefix sums from index $2$ to $2 \times \textit{limit}$, which is the minimum number of replacements needed.
-The time complexity is $O(n)$, and the space complexity is $O(n)$. Here, $n$ is the length of the array $nums$.
+The time complexity is $O(n)$, and the space complexity is $O(n)$. Here, $n$ is the length of the array $\textit{nums}$.
+
+Similar problems:
+
+- [3224. Minimum Array Changes to Make Differences Equal](https://github.com/doocs/leetcode/blob/main/solution/3200-3299/3224.Minimum%20Array%20Changes%20to%20Make%20Differences%20Equal/README_EN.md)
@@ -103,27 +109,20 @@ The time complexity is $O(n)$, and the space complexity is $O(n)$. Here, $n$ is
```python
class Solution:
def minMoves(self, nums: List[int], limit: int) -> int:
- d = [0] * (limit * 2 + 2)
+ d = [0] * (2 * limit + 2)
n = len(nums)
-
- for i in range(n >> 1):
- a, b = min(nums[i], nums[n - i - 1]), max(nums[i], nums[n - i - 1])
-
+ for i in range(n // 2):
+ x, y = nums[i], nums[-i - 1]
+ if x > y:
+ x, y = y, x
d[2] += 2
- d[limit * 2 + 1] -= 2
-
- d[a + 1] -= 1
- d[b + limit + 1] += 1
-
- d[a + b] -= 1
- d[a + b + 1] += 1
-
- ans, s = n, 0
- for v in d[2 : limit * 2 + 1]:
- s += v
- if ans > s:
- ans = s
- return ans
+ d[x + 1] -= 2
+ d[x + 1] += 1
+ d[x + y] -= 1
+ d[x + y + 1] += 1
+ d[y + limit + 1] -= 1
+ d[y + limit + 1] += 2
+ return min(accumulate(d[2:]))
```
#### Java
@@ -131,27 +130,23 @@ class Solution:
```java
class Solution {
public int minMoves(int[] nums, int limit) {
+ int[] d = new int[2 * limit + 2];
int n = nums.length;
- int[] d = new int[limit * 2 + 2];
- for (int i = 0; i < n >> 1; ++i) {
- int a = Math.min(nums[i], nums[n - i - 1]);
- int b = Math.max(nums[i], nums[n - i - 1]);
-
+ for (int i = 0; i < n / 2; ++i) {
+ int x = Math.min(nums[i], nums[n - i - 1]);
+ int y = Math.max(nums[i], nums[n - i - 1]);
d[2] += 2;
- d[limit * 2 + 1] -= 2;
-
- d[a + 1] -= 1;
- d[b + limit + 1] += 1;
-
- d[a + b] -= 1;
- d[a + b + 1] += 1;
+ d[x + 1] -= 2;
+ d[x + 1] += 1;
+ d[x + y] -= 1;
+ d[x + y + 1] += 1;
+ d[y + limit + 1] -= 1;
+ d[y + limit + 1] += 2;
}
- int ans = n, s = 0;
- for (int i = 2; i <= limit * 2; ++i) {
+ int ans = n;
+ for (int i = 2, s = 0; i < d.length; ++i) {
s += d[i];
- if (ans > s) {
- ans = s;
- }
+ ans = Math.min(ans, s);
}
return ans;
}
@@ -165,26 +160,25 @@ class Solution {
public:
int minMoves(vector& nums, int limit) {
int n = nums.size();
- vector d(limit * 2 + 2);
- for (int i = 0; i < n >> 1; ++i) {
- int a = min(nums[i], nums[n - i - 1]);
- int b = max(nums[i], nums[n - i - 1]);
-
+ int d[limit * 2 + 2];
+ memset(d, 0, sizeof(d));
+ for (int i = 0; i < n / 2; ++i) {
+ int x = nums[i], y = nums[n - i - 1];
+ if (x > y) {
+ swap(x, y);
+ }
d[2] += 2;
- d[limit * 2 + 1] -= 2;
-
- d[a + 1] -= 1;
- d[b + limit + 1] += 1;
-
- d[a + b] -= 1;
- d[a + b + 1] += 1;
+ d[x + 1] -= 2;
+ d[x + 1] += 1;
+ d[x + y] -= 1;
+ d[x + y + 1] += 1;
+ d[y + limit + 1] -= 1;
+ d[y + limit + 1] += 2;
}
- int ans = n, s = 0;
- for (int i = 2; i <= limit * 2; ++i) {
+ int ans = n;
+ for (int i = 2, s = 0; i <= limit * 2; ++i) {
s += d[i];
- if (ans > s) {
- ans = s;
- }
+ ans = min(ans, s);
}
return ans;
}
@@ -195,25 +189,25 @@ public:
```go
func minMoves(nums []int, limit int) int {
- d := make([]int, limit*2+2)
n := len(nums)
- for i := 0; i < n>>1; i++ {
- a, b := min(nums[i], nums[n-i-1]), max(nums[i], nums[n-i-1])
+ d := make([]int, 2*limit+2)
+ for i := 0; i < n/2; i++ {
+ x, y := nums[i], nums[n-1-i]
+ if x > y {
+ x, y = y, x
+ }
d[2] += 2
- d[limit*2+1] -= 2
-
- d[a+1] -= 1
- d[b+limit+1] += 1
-
- d[a+b] -= 1
- d[a+b+1] += 1
+ d[x+1] -= 2
+ d[x+1] += 1
+ d[x+y] -= 1
+ d[x+y+1] += 1
+ d[y+limit+1] -= 1
+ d[y+limit+1] += 2
}
ans, s := n, 0
- for _, v := range d[2 : limit*2+1] {
- s += v
- if ans > s {
- ans = s
- }
+ for _, x := range d[2:] {
+ s += x
+ ans = min(ans, s)
}
return ans
}
@@ -226,25 +220,21 @@ function minMoves(nums: number[], limit: number): number {
const n = nums.length;
const d: number[] = Array(limit * 2 + 2).fill(0);
for (let i = 0; i < n >> 1; ++i) {
- const a = Math.min(nums[i], nums[n - i - 1]);
- const b = Math.max(nums[i], nums[n - i - 1]);
-
+ const x = Math.min(nums[i], nums[n - 1 - i]);
+ const y = Math.max(nums[i], nums[n - 1 - i]);
d[2] += 2;
- d[limit * 2 + 1] -= 2;
-
- d[a + 1] -= 1;
- d[b + limit + 1] += 1;
-
- d[a + b] -= 1;
- d[a + b + 1] += 1;
+ d[x + 1] -= 2;
+ d[x + 1] += 1;
+ d[x + y] -= 1;
+ d[x + y + 1] += 1;
+ d[y + limit + 1] -= 1;
+ d[y + limit + 1] += 2;
}
let ans = n;
let s = 0;
- for (let i = 2; i <= limit * 2; ++i) {
+ for (let i = 2; i < d.length; ++i) {
s += d[i];
- if (ans > s) {
- ans = s;
- }
+ ans = Math.min(ans, s);
}
return ans;
}
diff --git a/solution/1600-1699/1674.Minimum Moves to Make Array Complementary/Solution.cpp b/solution/1600-1699/1674.Minimum Moves to Make Array Complementary/Solution.cpp
index 4fd464b0d54f6..06d2beb310785 100644
--- a/solution/1600-1699/1674.Minimum Moves to Make Array Complementary/Solution.cpp
+++ b/solution/1600-1699/1674.Minimum Moves to Make Array Complementary/Solution.cpp
@@ -2,26 +2,25 @@ class Solution {
public:
int minMoves(vector& nums, int limit) {
int n = nums.size();
- vector d(limit * 2 + 2);
- for (int i = 0; i < n >> 1; ++i) {
- int a = min(nums[i], nums[n - i - 1]);
- int b = max(nums[i], nums[n - i - 1]);
-
+ int d[limit * 2 + 2];
+ memset(d, 0, sizeof(d));
+ for (int i = 0; i < n / 2; ++i) {
+ int x = nums[i], y = nums[n - i - 1];
+ if (x > y) {
+ swap(x, y);
+ }
d[2] += 2;
- d[limit * 2 + 1] -= 2;
-
- d[a + 1] -= 1;
- d[b + limit + 1] += 1;
-
- d[a + b] -= 1;
- d[a + b + 1] += 1;
+ d[x + 1] -= 2;
+ d[x + 1] += 1;
+ d[x + y] -= 1;
+ d[x + y + 1] += 1;
+ d[y + limit + 1] -= 1;
+ d[y + limit + 1] += 2;
}
- int ans = n, s = 0;
- for (int i = 2; i <= limit * 2; ++i) {
+ int ans = n;
+ for (int i = 2, s = 0; i <= limit * 2; ++i) {
s += d[i];
- if (ans > s) {
- ans = s;
- }
+ ans = min(ans, s);
}
return ans;
}
diff --git a/solution/1600-1699/1674.Minimum Moves to Make Array Complementary/Solution.go b/solution/1600-1699/1674.Minimum Moves to Make Array Complementary/Solution.go
index 98ecd9d1bd84f..77135e1f86f80 100644
--- a/solution/1600-1699/1674.Minimum Moves to Make Array Complementary/Solution.go
+++ b/solution/1600-1699/1674.Minimum Moves to Make Array Complementary/Solution.go
@@ -1,23 +1,23 @@
func minMoves(nums []int, limit int) int {
- d := make([]int, limit*2+2)
n := len(nums)
- for i := 0; i < n>>1; i++ {
- a, b := min(nums[i], nums[n-i-1]), max(nums[i], nums[n-i-1])
+ d := make([]int, 2*limit+2)
+ for i := 0; i < n/2; i++ {
+ x, y := nums[i], nums[n-1-i]
+ if x > y {
+ x, y = y, x
+ }
d[2] += 2
- d[limit*2+1] -= 2
-
- d[a+1] -= 1
- d[b+limit+1] += 1
-
- d[a+b] -= 1
- d[a+b+1] += 1
+ d[x+1] -= 2
+ d[x+1] += 1
+ d[x+y] -= 1
+ d[x+y+1] += 1
+ d[y+limit+1] -= 1
+ d[y+limit+1] += 2
}
ans, s := n, 0
- for _, v := range d[2 : limit*2+1] {
- s += v
- if ans > s {
- ans = s
- }
+ for _, x := range d[2:] {
+ s += x
+ ans = min(ans, s)
}
return ans
}
\ No newline at end of file
diff --git a/solution/1600-1699/1674.Minimum Moves to Make Array Complementary/Solution.java b/solution/1600-1699/1674.Minimum Moves to Make Array Complementary/Solution.java
index 44f2f7df6af80..ff415a4cad2b1 100644
--- a/solution/1600-1699/1674.Minimum Moves to Make Array Complementary/Solution.java
+++ b/solution/1600-1699/1674.Minimum Moves to Make Array Complementary/Solution.java
@@ -1,26 +1,22 @@
class Solution {
public int minMoves(int[] nums, int limit) {
+ int[] d = new int[2 * limit + 2];
int n = nums.length;
- int[] d = new int[limit * 2 + 2];
- for (int i = 0; i < n >> 1; ++i) {
- int a = Math.min(nums[i], nums[n - i - 1]);
- int b = Math.max(nums[i], nums[n - i - 1]);
-
+ for (int i = 0; i < n / 2; ++i) {
+ int x = Math.min(nums[i], nums[n - i - 1]);
+ int y = Math.max(nums[i], nums[n - i - 1]);
d[2] += 2;
- d[limit * 2 + 1] -= 2;
-
- d[a + 1] -= 1;
- d[b + limit + 1] += 1;
-
- d[a + b] -= 1;
- d[a + b + 1] += 1;
+ d[x + 1] -= 2;
+ d[x + 1] += 1;
+ d[x + y] -= 1;
+ d[x + y + 1] += 1;
+ d[y + limit + 1] -= 1;
+ d[y + limit + 1] += 2;
}
- int ans = n, s = 0;
- for (int i = 2; i <= limit * 2; ++i) {
+ int ans = n;
+ for (int i = 2, s = 0; i < d.length; ++i) {
s += d[i];
- if (ans > s) {
- ans = s;
- }
+ ans = Math.min(ans, s);
}
return ans;
}
diff --git a/solution/1600-1699/1674.Minimum Moves to Make Array Complementary/Solution.py b/solution/1600-1699/1674.Minimum Moves to Make Array Complementary/Solution.py
index 866fcf6fb3a5c..6ed848e047572 100644
--- a/solution/1600-1699/1674.Minimum Moves to Make Array Complementary/Solution.py
+++ b/solution/1600-1699/1674.Minimum Moves to Make Array Complementary/Solution.py
@@ -1,23 +1,16 @@
class Solution:
def minMoves(self, nums: List[int], limit: int) -> int:
- d = [0] * (limit * 2 + 2)
+ d = [0] * (2 * limit + 2)
n = len(nums)
-
- for i in range(n >> 1):
- a, b = min(nums[i], nums[n - i - 1]), max(nums[i], nums[n - i - 1])
-
+ for i in range(n // 2):
+ x, y = nums[i], nums[-i - 1]
+ if x > y:
+ x, y = y, x
d[2] += 2
- d[limit * 2 + 1] -= 2
-
- d[a + 1] -= 1
- d[b + limit + 1] += 1
-
- d[a + b] -= 1
- d[a + b + 1] += 1
-
- ans, s = n, 0
- for v in d[2 : limit * 2 + 1]:
- s += v
- if ans > s:
- ans = s
- return ans
+ d[x + 1] -= 2
+ d[x + 1] += 1
+ d[x + y] -= 1
+ d[x + y + 1] += 1
+ d[y + limit + 1] -= 1
+ d[y + limit + 1] += 2
+ return min(accumulate(d[2:]))
diff --git a/solution/1600-1699/1674.Minimum Moves to Make Array Complementary/Solution.ts b/solution/1600-1699/1674.Minimum Moves to Make Array Complementary/Solution.ts
index 4a8b405cb8569..6e45ff4da48e8 100644
--- a/solution/1600-1699/1674.Minimum Moves to Make Array Complementary/Solution.ts
+++ b/solution/1600-1699/1674.Minimum Moves to Make Array Complementary/Solution.ts
@@ -2,25 +2,21 @@ function minMoves(nums: number[], limit: number): number {
const n = nums.length;
const d: number[] = Array(limit * 2 + 2).fill(0);
for (let i = 0; i < n >> 1; ++i) {
- const a = Math.min(nums[i], nums[n - i - 1]);
- const b = Math.max(nums[i], nums[n - i - 1]);
-
+ const x = Math.min(nums[i], nums[n - 1 - i]);
+ const y = Math.max(nums[i], nums[n - 1 - i]);
d[2] += 2;
- d[limit * 2 + 1] -= 2;
-
- d[a + 1] -= 1;
- d[b + limit + 1] += 1;
-
- d[a + b] -= 1;
- d[a + b + 1] += 1;
+ d[x + 1] -= 2;
+ d[x + 1] += 1;
+ d[x + y] -= 1;
+ d[x + y + 1] += 1;
+ d[y + limit + 1] -= 1;
+ d[y + limit + 1] += 2;
}
let ans = n;
let s = 0;
- for (let i = 2; i <= limit * 2; ++i) {
+ for (let i = 2; i < d.length; ++i) {
s += d[i];
- if (ans > s) {
- ans = s;
- }
+ ans = Math.min(ans, s);
}
return ans;
}
diff --git a/solution/3200-3299/3224.Minimum Array Changes to Make Differences Equal/README.md b/solution/3200-3299/3224.Minimum Array Changes to Make Differences Equal/README.md
index fe3341743ccab..c32559d8f35b5 100644
--- a/solution/3200-3299/3224.Minimum Array Changes to Make Differences Equal/README.md
+++ b/solution/3200-3299/3224.Minimum Array Changes to Make Differences Equal/README.md
@@ -80,32 +80,158 @@ edit_url: https://github.com/doocs/leetcode/edit/main/solution/3200-3299/3224.Mi
-### 方法一
+### 方法一:差分数组
+
+假设最终的数组中,数对 $\textit{nums}[i]$ 和 $\textit{nums}[n-i-1]$ 的差值为 $s$。
+
+我们不妨设 $x$ 为 $\textit{nums}[i]$ 和 $\textit{nums}[n-i-1]$ 的较小值,设 $y$ 为 $\textit{nums}[i]$ 和 $\textit{nums}[n-i-1]$ 的较大值。
+
+对于每一对数,我们有以下几种情况:
+
+- 如果不需要改动,那么 $y - x = s$。
+- 如果改动一次,那么 $s \le \max(y, k - x)$,最大值就是把 $x$ 变为 $0$,或者把 $y$ 变为 $k$。
+- 如果改动两次,那么 $s \gt \max(y, k - x)$。
+
+即:
+
+- 在 $[0,y-x-1]$ 范围内,需要改动 $1$ 次。
+- 在 $[y-x]$ 时,不需要改动。
+- 在 $[y-x+1, \max(y, k-x)]$ 范围内,需要改动 $1$ 次。
+- 在 $[\max(y, k-x)+1, k]$ 范围内,需要改动 $2$ 次。
+
+我们枚举每一个数对,利用差分数组,更新每个数对在不同区间范围内的改动次数。
+
+最后,我们求出差分数组的前缀和中的最小值,即为最少的改动次数。
+
+时间复杂度 $O(n)$,空间复杂度 $O(n)$。其中 $n$ 为数组 $\textit{nums}$ 的长度。
+
+相似题目:
+
+- [1674. 使数组互补的最少操作次数](https://github.com/doocs/leetcode/tree/main/solution/1600-1699/1674.Minimum%20Moves%20to%20Make%20Array%20Complementary/README.md)
#### Python3
```python
-
+class Solution:
+ def minChanges(self, nums: List[int], k: int) -> int:
+ d = [0] * (k + 2)
+ n = len(nums)
+ for i in range(n // 2):
+ x, y = nums[i], nums[-i - 1]
+ if x > y:
+ x, y = y, x
+ d[0] += 1
+ d[y - x] -= 1
+ d[y - x + 1] += 1
+ d[max(y, k - x) + 1] -= 1
+ d[max(y, k - x) + 1] += 2
+ return min(accumulate(d))
```
#### Java
```java
-
+class Solution {
+ public int minChanges(int[] nums, int k) {
+ int[] d = new int[k + 2];
+ int n = nums.length;
+ for (int i = 0; i < n / 2; ++i) {
+ int x = Math.min(nums[i], nums[n - i - 1]);
+ int y = Math.max(nums[i], nums[n - i - 1]);
+ d[0] += 1;
+ d[y - x] -= 1;
+ d[y - x + 1] += 1;
+ d[Math.max(y, k - x) + 1] -= 1;
+ d[Math.max(y, k - x) + 1] += 2;
+ }
+ int ans = n, s = 0;
+ for (int x : d) {
+ s += x;
+ ans = Math.min(ans, s);
+ }
+ return ans;
+ }
+}
```
#### C++
```cpp
-
+class Solution {
+public:
+ int minChanges(vector& nums, int k) {
+ int d[k + 2];
+ memset(d, 0, sizeof(d));
+ int n = nums.size();
+ for (int i = 0; i < n / 2; ++i) {
+ int x = min(nums[i], nums[n - i - 1]);
+ int y = max(nums[i], nums[n - i - 1]);
+ d[0] += 1;
+ d[y - x] -= 1;
+ d[y - x + 1] += 1;
+ d[max(y, k - x) + 1] -= 1;
+ d[max(y, k - x) + 1] += 2;
+ }
+ int ans = n, s = 0;
+ for (int x : d) {
+ s += x;
+ ans = min(ans, s);
+ }
+ return ans;
+ }
+};
```
#### Go
```go
+func minChanges(nums []int, k int) int {
+ d := make([]int, k+2)
+ n := len(nums)
+ for i := 0; i < n/2; i++ {
+ x, y := nums[i], nums[n-1-i]
+ if x > y {
+ x, y = y, x
+ }
+ d[0] += 1
+ d[y-x] -= 1
+ d[y-x+1] += 1
+ d[max(y, k-x)+1] -= 1
+ d[max(y, k-x)+1] += 2
+ }
+ ans, s := n, 0
+ for _, x := range d {
+ s += x
+ ans = min(ans, s)
+ }
+ return ans
+}
+```
+#### TypeScript
+
+```ts
+function minChanges(nums: number[], k: number): number {
+ const d: number[] = Array(k + 2).fill(0);
+ const n = nums.length;
+ for (let i = 0; i < n >> 1; ++i) {
+ const x = Math.min(nums[i], nums[n - 1 - i]);
+ const y = Math.max(nums[i], nums[n - 1 - i]);
+ d[0] += 1;
+ d[y - x] -= 1;
+ d[y - x + 1] += 1;
+ d[Math.max(y, k - x) + 1] -= 1;
+ d[Math.max(y, k - x) + 1] += 2;
+ }
+ let [ans, s] = [n, 0];
+ for (const x of d) {
+ s += x;
+ ans = Math.min(ans, s);
+ }
+ return ans;
+}
```
diff --git a/solution/3200-3299/3224.Minimum Array Changes to Make Differences Equal/README_EN.md b/solution/3200-3299/3224.Minimum Array Changes to Make Differences Equal/README_EN.md
index c3d8ef99586cb..1336d565f7735 100644
--- a/solution/3200-3299/3224.Minimum Array Changes to Make Differences Equal/README_EN.md
+++ b/solution/3200-3299/3224.Minimum Array Changes to Make Differences Equal/README_EN.md
@@ -78,32 +78,158 @@ We can perform the following operations:
-### Solution 1
+### Solution 1: Difference Array
+
+Assume that in the final array, the difference between the pair $\textit{nums}[i]$ and $\textit{nums}[n-i-1]$ is $s$.
+
+Let's denote $x$ as the smaller value between $\textit{nums}[i]$ and $\textit{nums}[n-i-1]$, and $y$ as the larger value.
+
+For each pair of numbers, we have the following scenarios:
+
+- If no change is needed, then $y - x = s$.
+- If one change is made, then $s \le \max(y, k - x)$, where the maximum value is achieved by changing $x$ to $0$, or changing $y$ to $k$.
+- If two changes are made, then $s > \max(y, k - x)$.
+
+That is:
+
+- In the range $[0, y-x-1]$, $1$ change is needed.
+- At $[y-x]$, no change is needed.
+- In the range $[y-x+1, \max(y, k-x)]$, $1$ change is needed.
+- In the range $[\max(y, k-x)+1, k]$, $2$ changes are needed.
+
+We enumerate each pair of numbers and use a difference array to update the number of changes needed in different ranges for each pair.
+
+Finally, we find the minimum value among the prefix sums from the difference array, which is the minimum number of changes needed.
+
+The time complexity is $O(n)$, and the space complexity is $O(n)$. Here, $n$ is the length of the array $\textit{nums}$.
+
+Similar problems:
+
+- [1674. Minimum Moves to Make Array Complementary](https://github.com/doocs/leetcode/tree/main/solution/1600-1699/1674.Minimum%20Moves%20to%20Make%20Array%20Complementary/README_EN.md)
#### Python3
```python
-
+class Solution:
+ def minChanges(self, nums: List[int], k: int) -> int:
+ d = [0] * (k + 2)
+ n = len(nums)
+ for i in range(n // 2):
+ x, y = nums[i], nums[-i - 1]
+ if x > y:
+ x, y = y, x
+ d[0] += 1
+ d[y - x] -= 1
+ d[y - x + 1] += 1
+ d[max(y, k - x) + 1] -= 1
+ d[max(y, k - x) + 1] += 2
+ return min(accumulate(d))
```
#### Java
```java
-
+class Solution {
+ public int minChanges(int[] nums, int k) {
+ int[] d = new int[k + 2];
+ int n = nums.length;
+ for (int i = 0; i < n / 2; ++i) {
+ int x = Math.min(nums[i], nums[n - i - 1]);
+ int y = Math.max(nums[i], nums[n - i - 1]);
+ d[0] += 1;
+ d[y - x] -= 1;
+ d[y - x + 1] += 1;
+ d[Math.max(y, k - x) + 1] -= 1;
+ d[Math.max(y, k - x) + 1] += 2;
+ }
+ int ans = n, s = 0;
+ for (int x : d) {
+ s += x;
+ ans = Math.min(ans, s);
+ }
+ return ans;
+ }
+}
```
#### C++
```cpp
-
+class Solution {
+public:
+ int minChanges(vector& nums, int k) {
+ int d[k + 2];
+ memset(d, 0, sizeof(d));
+ int n = nums.size();
+ for (int i = 0; i < n / 2; ++i) {
+ int x = min(nums[i], nums[n - i - 1]);
+ int y = max(nums[i], nums[n - i - 1]);
+ d[0] += 1;
+ d[y - x] -= 1;
+ d[y - x + 1] += 1;
+ d[max(y, k - x) + 1] -= 1;
+ d[max(y, k - x) + 1] += 2;
+ }
+ int ans = n, s = 0;
+ for (int x : d) {
+ s += x;
+ ans = min(ans, s);
+ }
+ return ans;
+ }
+};
```
#### Go
```go
+func minChanges(nums []int, k int) int {
+ d := make([]int, k+2)
+ n := len(nums)
+ for i := 0; i < n/2; i++ {
+ x, y := nums[i], nums[n-1-i]
+ if x > y {
+ x, y = y, x
+ }
+ d[0] += 1
+ d[y-x] -= 1
+ d[y-x+1] += 1
+ d[max(y, k-x)+1] -= 1
+ d[max(y, k-x)+1] += 2
+ }
+ ans, s := n, 0
+ for _, x := range d {
+ s += x
+ ans = min(ans, s)
+ }
+ return ans
+}
+```
+#### TypeScript
+
+```ts
+function minChanges(nums: number[], k: number): number {
+ const d: number[] = Array(k + 2).fill(0);
+ const n = nums.length;
+ for (let i = 0; i < n >> 1; ++i) {
+ const x = Math.min(nums[i], nums[n - 1 - i]);
+ const y = Math.max(nums[i], nums[n - 1 - i]);
+ d[0] += 1;
+ d[y - x] -= 1;
+ d[y - x + 1] += 1;
+ d[Math.max(y, k - x) + 1] -= 1;
+ d[Math.max(y, k - x) + 1] += 2;
+ }
+ let [ans, s] = [n, 0];
+ for (const x of d) {
+ s += x;
+ ans = Math.min(ans, s);
+ }
+ return ans;
+}
```
diff --git a/solution/3200-3299/3224.Minimum Array Changes to Make Differences Equal/Solution.cpp b/solution/3200-3299/3224.Minimum Array Changes to Make Differences Equal/Solution.cpp
new file mode 100644
index 0000000000000..696bfb8b7de53
--- /dev/null
+++ b/solution/3200-3299/3224.Minimum Array Changes to Make Differences Equal/Solution.cpp
@@ -0,0 +1,23 @@
+class Solution {
+public:
+ int minChanges(vector& nums, int k) {
+ int d[k + 2];
+ memset(d, 0, sizeof(d));
+ int n = nums.size();
+ for (int i = 0; i < n / 2; ++i) {
+ int x = min(nums[i], nums[n - i - 1]);
+ int y = max(nums[i], nums[n - i - 1]);
+ d[0] += 1;
+ d[y - x] -= 1;
+ d[y - x + 1] += 1;
+ d[max(y, k - x) + 1] -= 1;
+ d[max(y, k - x) + 1] += 2;
+ }
+ int ans = n, s = 0;
+ for (int x : d) {
+ s += x;
+ ans = min(ans, s);
+ }
+ return ans;
+ }
+};
\ No newline at end of file
diff --git a/solution/3200-3299/3224.Minimum Array Changes to Make Differences Equal/Solution.go b/solution/3200-3299/3224.Minimum Array Changes to Make Differences Equal/Solution.go
new file mode 100644
index 0000000000000..957b8167e1e1b
--- /dev/null
+++ b/solution/3200-3299/3224.Minimum Array Changes to Make Differences Equal/Solution.go
@@ -0,0 +1,21 @@
+func minChanges(nums []int, k int) int {
+ d := make([]int, k+2)
+ n := len(nums)
+ for i := 0; i < n/2; i++ {
+ x, y := nums[i], nums[n-1-i]
+ if x > y {
+ x, y = y, x
+ }
+ d[0] += 1
+ d[y-x] -= 1
+ d[y-x+1] += 1
+ d[max(y, k-x)+1] -= 1
+ d[max(y, k-x)+1] += 2
+ }
+ ans, s := n, 0
+ for _, x := range d {
+ s += x
+ ans = min(ans, s)
+ }
+ return ans
+}
\ No newline at end of file
diff --git a/solution/3200-3299/3224.Minimum Array Changes to Make Differences Equal/Solution.java b/solution/3200-3299/3224.Minimum Array Changes to Make Differences Equal/Solution.java
new file mode 100644
index 0000000000000..70e0610c73c26
--- /dev/null
+++ b/solution/3200-3299/3224.Minimum Array Changes to Make Differences Equal/Solution.java
@@ -0,0 +1,21 @@
+class Solution {
+ public int minChanges(int[] nums, int k) {
+ int[] d = new int[k + 2];
+ int n = nums.length;
+ for (int i = 0; i < n / 2; ++i) {
+ int x = Math.min(nums[i], nums[n - i - 1]);
+ int y = Math.max(nums[i], nums[n - i - 1]);
+ d[0] += 1;
+ d[y - x] -= 1;
+ d[y - x + 1] += 1;
+ d[Math.max(y, k - x) + 1] -= 1;
+ d[Math.max(y, k - x) + 1] += 2;
+ }
+ int ans = n, s = 0;
+ for (int x : d) {
+ s += x;
+ ans = Math.min(ans, s);
+ }
+ return ans;
+ }
+}
\ No newline at end of file
diff --git a/solution/3200-3299/3224.Minimum Array Changes to Make Differences Equal/Solution.py b/solution/3200-3299/3224.Minimum Array Changes to Make Differences Equal/Solution.py
new file mode 100644
index 0000000000000..a310c37ee734e
--- /dev/null
+++ b/solution/3200-3299/3224.Minimum Array Changes to Make Differences Equal/Solution.py
@@ -0,0 +1,14 @@
+class Solution:
+ def minChanges(self, nums: List[int], k: int) -> int:
+ d = [0] * (k + 2)
+ n = len(nums)
+ for i in range(n // 2):
+ x, y = nums[i], nums[-i - 1]
+ if x > y:
+ x, y = y, x
+ d[0] += 1
+ d[y - x] -= 1
+ d[y - x + 1] += 1
+ d[max(y, k - x) + 1] -= 1
+ d[max(y, k - x) + 1] += 2
+ return min(accumulate(d))
diff --git a/solution/3200-3299/3224.Minimum Array Changes to Make Differences Equal/Solution.ts b/solution/3200-3299/3224.Minimum Array Changes to Make Differences Equal/Solution.ts
new file mode 100644
index 0000000000000..2817d3baae1a3
--- /dev/null
+++ b/solution/3200-3299/3224.Minimum Array Changes to Make Differences Equal/Solution.ts
@@ -0,0 +1,19 @@
+function minChanges(nums: number[], k: number): number {
+ const d: number[] = Array(k + 2).fill(0);
+ const n = nums.length;
+ for (let i = 0; i < n >> 1; ++i) {
+ const x = Math.min(nums[i], nums[n - 1 - i]);
+ const y = Math.max(nums[i], nums[n - 1 - i]);
+ d[0] += 1;
+ d[y - x] -= 1;
+ d[y - x + 1] += 1;
+ d[Math.max(y, k - x) + 1] -= 1;
+ d[Math.max(y, k - x) + 1] += 2;
+ }
+ let [ans, s] = [n, 0];
+ for (const x of d) {
+ s += x;
+ ans = Math.min(ans, s);
+ }
+ return ans;
+}
diff --git a/solution/3200-3299/3228.Maximum Number of Operations to Move Ones to the End/README.md b/solution/3200-3299/3228.Maximum Number of Operations to Move Ones to the End/README.md
index 1e338042d8a25..5c33c98846b34 100644
--- a/solution/3200-3299/3228.Maximum Number of Operations to Move Ones to the End/README.md
+++ b/solution/3200-3299/3228.Maximum Number of Operations to Move Ones to the End/README.md
@@ -69,32 +69,102 @@ edit_url: https://github.com/doocs/leetcode/edit/main/solution/3200-3299/3228.Ma
-### 方法一
+### 方法一:贪心
+
+我们用一个变量 $\textit{ans}$ 记录答案,用一个变量 $\textit{cnt}$ 记录当前的 $1$ 的个数。
+
+然后我们遍历字符串 $s$,如果当前字符是 $1$,则 $\textit{cnt}$ 加一,否则如果存在前一个字符,且前一个字符是 $1$,那么前面的 $\textit{cnt}$ 个 $1$ 可以往后移动,答案加上 $\textit{cnt}$。
+
+最后返回答案即可。
+
+时间复杂度 $O(n)$,其中 $n$ 为字符串 $s$ 的长度。空间复杂度 $O(1)$。
#### Python3
```python
-
+class Solution:
+ def maxOperations(self, s: str) -> int:
+ ans = cnt = 0
+ for i, c in enumerate(s):
+ if c == "1":
+ cnt += 1
+ elif i and s[i - 1] == "1":
+ ans += cnt
+ return ans
```
#### Java
```java
-
+class Solution {
+ public int maxOperations(String s) {
+ int ans = 0, cnt = 0;
+ int n = s.length();
+ for (int i = 0; i < n; ++i) {
+ if (s.charAt(i) == '1') {
+ ++cnt;
+ } else if (i > 0 && s.charAt(i - 1) == '1') {
+ ans += cnt;
+ }
+ }
+ return ans;
+ }
+}
```
#### C++
```cpp
-
+class Solution {
+public:
+ int maxOperations(string s) {
+ int ans = 0, cnt = 0;
+ int n = s.size();
+ for (int i = 0; i < n; ++i) {
+ if (s[i] == '1') {
+ ++cnt;
+ } else if (i && s[i - 1] == '1') {
+ ans += cnt;
+ }
+ }
+ return ans;
+ }
+};
```
#### Go
```go
+func maxOperations(s string) (ans int) {
+ cnt := 0
+ for i, c := range s {
+ if c == '1' {
+ cnt++
+ } else if i > 0 && s[i-1] == '1' {
+ ans += cnt
+ }
+ }
+ return
+}
+```
+#### TypeScript
+
+```ts
+function maxOperations(s: string): number {
+ let [ans, cnt] = [0, 0];
+ const n = s.length;
+ for (let i = 0; i < n; ++i) {
+ if (s[i] === '1') {
+ ++cnt;
+ } else if (i && s[i - 1] === '1') {
+ ans += cnt;
+ }
+ }
+ return ans;
+}
```
diff --git a/solution/3200-3299/3228.Maximum Number of Operations to Move Ones to the End/README_EN.md b/solution/3200-3299/3228.Maximum Number of Operations to Move Ones to the End/README_EN.md
index 69e9fc85fc9d0..4135cc9bab56c 100644
--- a/solution/3200-3299/3228.Maximum Number of Operations to Move Ones to the End/README_EN.md
+++ b/solution/3200-3299/3228.Maximum Number of Operations to Move Ones to the End/README_EN.md
@@ -67,32 +67,102 @@ edit_url: https://github.com/doocs/leetcode/edit/main/solution/3200-3299/3228.Ma
-### Solution 1
+### Solution 1: Greedy
+
+We use a variable $\textit{ans}$ to record the answer and another variable $\textit{cnt}$ to count the current number of $1$s.
+
+Then, we iterate through the string $s$. If the current character is $1$, then we increment $\textit{cnt}$. Otherwise, if there is a previous character and the previous character is $1$, then the previous $\textit{cnt}$ number of $1$s can be moved backward, and we add $\textit{cnt}$ to the answer.
+
+Finally, we return the answer.
+
+The time complexity is $O(n)$, where $n$ is the length of the string $s$. The space complexity is $O(1)$.
#### Python3
```python
-
+class Solution:
+ def maxOperations(self, s: str) -> int:
+ ans = cnt = 0
+ for i, c in enumerate(s):
+ if c == "1":
+ cnt += 1
+ elif i and s[i - 1] == "1":
+ ans += cnt
+ return ans
```
#### Java
```java
-
+class Solution {
+ public int maxOperations(String s) {
+ int ans = 0, cnt = 0;
+ int n = s.length();
+ for (int i = 0; i < n; ++i) {
+ if (s.charAt(i) == '1') {
+ ++cnt;
+ } else if (i > 0 && s.charAt(i - 1) == '1') {
+ ans += cnt;
+ }
+ }
+ return ans;
+ }
+}
```
#### C++
```cpp
-
+class Solution {
+public:
+ int maxOperations(string s) {
+ int ans = 0, cnt = 0;
+ int n = s.size();
+ for (int i = 0; i < n; ++i) {
+ if (s[i] == '1') {
+ ++cnt;
+ } else if (i && s[i - 1] == '1') {
+ ans += cnt;
+ }
+ }
+ return ans;
+ }
+};
```
#### Go
```go
+func maxOperations(s string) (ans int) {
+ cnt := 0
+ for i, c := range s {
+ if c == '1' {
+ cnt++
+ } else if i > 0 && s[i-1] == '1' {
+ ans += cnt
+ }
+ }
+ return
+}
+```
+#### TypeScript
+
+```ts
+function maxOperations(s: string): number {
+ let [ans, cnt] = [0, 0];
+ const n = s.length;
+ for (let i = 0; i < n; ++i) {
+ if (s[i] === '1') {
+ ++cnt;
+ } else if (i && s[i - 1] === '1') {
+ ans += cnt;
+ }
+ }
+ return ans;
+}
```
diff --git a/solution/3200-3299/3228.Maximum Number of Operations to Move Ones to the End/Solution.cpp b/solution/3200-3299/3228.Maximum Number of Operations to Move Ones to the End/Solution.cpp
new file mode 100644
index 0000000000000..e76afa9f8ddf8
--- /dev/null
+++ b/solution/3200-3299/3228.Maximum Number of Operations to Move Ones to the End/Solution.cpp
@@ -0,0 +1,15 @@
+class Solution {
+public:
+ int maxOperations(string s) {
+ int ans = 0, cnt = 0;
+ int n = s.size();
+ for (int i = 0; i < n; ++i) {
+ if (s[i] == '1') {
+ ++cnt;
+ } else if (i && s[i - 1] == '1') {
+ ans += cnt;
+ }
+ }
+ return ans;
+ }
+};
\ No newline at end of file
diff --git a/solution/3200-3299/3228.Maximum Number of Operations to Move Ones to the End/Solution.go b/solution/3200-3299/3228.Maximum Number of Operations to Move Ones to the End/Solution.go
new file mode 100644
index 0000000000000..c30a2c1ab40e1
--- /dev/null
+++ b/solution/3200-3299/3228.Maximum Number of Operations to Move Ones to the End/Solution.go
@@ -0,0 +1,11 @@
+func maxOperations(s string) (ans int) {
+ cnt := 0
+ for i, c := range s {
+ if c == '1' {
+ cnt++
+ } else if i > 0 && s[i-1] == '1' {
+ ans += cnt
+ }
+ }
+ return
+}
\ No newline at end of file
diff --git a/solution/3200-3299/3228.Maximum Number of Operations to Move Ones to the End/Solution.java b/solution/3200-3299/3228.Maximum Number of Operations to Move Ones to the End/Solution.java
new file mode 100644
index 0000000000000..0b95971b06c51
--- /dev/null
+++ b/solution/3200-3299/3228.Maximum Number of Operations to Move Ones to the End/Solution.java
@@ -0,0 +1,14 @@
+class Solution {
+ public int maxOperations(String s) {
+ int ans = 0, cnt = 0;
+ int n = s.length();
+ for (int i = 0; i < n; ++i) {
+ if (s.charAt(i) == '1') {
+ ++cnt;
+ } else if (i > 0 && s.charAt(i - 1) == '1') {
+ ans += cnt;
+ }
+ }
+ return ans;
+ }
+}
\ No newline at end of file
diff --git a/solution/3200-3299/3228.Maximum Number of Operations to Move Ones to the End/Solution.py b/solution/3200-3299/3228.Maximum Number of Operations to Move Ones to the End/Solution.py
new file mode 100644
index 0000000000000..63098db43f637
--- /dev/null
+++ b/solution/3200-3299/3228.Maximum Number of Operations to Move Ones to the End/Solution.py
@@ -0,0 +1,9 @@
+class Solution:
+ def maxOperations(self, s: str) -> int:
+ ans = cnt = 0
+ for i, c in enumerate(s):
+ if c == "1":
+ cnt += 1
+ elif i and s[i - 1] == "1":
+ ans += cnt
+ return ans
diff --git a/solution/3200-3299/3228.Maximum Number of Operations to Move Ones to the End/Solution.ts b/solution/3200-3299/3228.Maximum Number of Operations to Move Ones to the End/Solution.ts
new file mode 100644
index 0000000000000..1e294a779a338
--- /dev/null
+++ b/solution/3200-3299/3228.Maximum Number of Operations to Move Ones to the End/Solution.ts
@@ -0,0 +1,12 @@
+function maxOperations(s: string): number {
+ let [ans, cnt] = [0, 0];
+ const n = s.length;
+ for (let i = 0; i < n; ++i) {
+ if (s[i] === '1') {
+ ++cnt;
+ } else if (i && s[i - 1] === '1') {
+ ans += cnt;
+ }
+ }
+ return ans;
+}