Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit f02a68f

Browse files
committed
feat: add solutions to lc problem: No.1551.Minimum Operations to Make Array Equal
1 parent 7fd5bf8 commit f02a68f

File tree

6 files changed

+103
-22
lines changed

6 files changed

+103
-22
lines changed

‎solution/1500-1599/1551.Minimum Operations to Make Array Equal/README.md‎

Lines changed: 41 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,27 +37,66 @@
3737
<li><code>1 &lt;= n &lt;= 10^4</code></li>
3838
</ul>
3939

40-
4140
## 解法
4241

4342
<!-- 这里可写通用的实现逻辑 -->
4443

44+
数组 arr 的前 n 项和为 `(1 + (2 * n - 1)) * n / 2 = n * n`,若数组所有元素相等,那么每一项元素应该都是 n,因此只需累计数组前半部分的元素操作次数 `n - (2 * i + 1)` 即可,即 n ∈ `[0, n / 2)`
45+
4546
<!-- tabs:start -->
4647

4748
### **Python3**
4849

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

5152
```python
52-
53+
class Solution:
54+
def minOperations(self, n: int) -> int:
55+
ans = 0
56+
for i in range(n >> 1):
57+
ans += (n - (2 * i + 1))
58+
return ans
5359
```
5460

5561
### **Java**
5662

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

5965
```java
66+
class Solution {
67+
public int minOperations(int n) {
68+
int ans = 0;
69+
for (int i = 0; i < (n >> 1); i++) {
70+
ans += (n - (2 * i + 1));
71+
}
72+
return ans;
73+
}
74+
}
75+
```
76+
77+
### **C++**
78+
79+
```cpp
80+
class Solution {
81+
public:
82+
int minOperations(int n) {
83+
int ans = 0;
84+
for (int i = 0; i < (n >> 1); ++i) ans += (n - (2 * i + 1));
85+
return ans;
86+
}
87+
};
88+
```
89+
90+
### **Go**
6091
92+
```go
93+
func minOperations(n int) int {
94+
ans := 0
95+
for i := 0; i < (n >> 1); i++ {
96+
ans += (n - (2*i + 1))
97+
}
98+
return ans
99+
}
61100
```
62101

63102
### **...**

‎solution/1500-1599/1551.Minimum Operations to Make Array Equal/README_EN.md‎

Lines changed: 39 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -6,22 +6,14 @@
66

77
<p>You have an array <code>arr</code> of length <code>n</code> where <code>arr[i] = (2 * i) + 1</code> for all valid values of <code>i</code> (i.e. <code>0 &lt;= i &lt; n</code>).</p>
88

9-
10-
119
<p>In one operation, you can select two indices <code>x</code>&nbsp;and <code>y</code> where <code>0 &lt;= x, y &lt; n</code> and subtract <code>1</code> from <code>arr[x]</code> and add <code>1</code> to <code>arr[y]</code>&nbsp;(i.e. perform <code>arr[x] -=1&nbsp;</code>and <code>arr[y] += 1</code>).&nbsp;The goal is to make all the elements of the array <strong>equal</strong>. It is <strong>guaranteed</strong> that all the elements of the array can be made equal using some operations.</p>
1210

13-
14-
1511
<p>Given an integer <code>n</code>, the length of the array. Return <em>the minimum number of operations</em> needed to make&nbsp;all the elements of arr equal.</p>
1612

17-
18-
1913
<p>&nbsp;</p>
2014

2115
<p><strong>Example 1:</strong></p>
2216

23-
24-
2517
<pre>
2618

2719
<strong>Input:</strong> n = 3
@@ -36,12 +28,8 @@ In the second operation choose x = 2 and y = 0 again, thus arr = [3, 3, 3].
3628

3729
</pre>
3830

39-
40-
4131
<p><strong>Example 2:</strong></p>
4232

43-
44-
4533
<pre>
4634

4735
<strong>Input:</strong> n = 6
@@ -50,14 +38,10 @@ In the second operation choose x = 2 and y = 0 again, thus arr = [3, 3, 3].
5038

5139
</pre>
5240

53-
54-
5541
<p>&nbsp;</p>
5642

5743
<p><strong>Constraints:</strong></p>
5844

59-
60-
6145
<ul>
6246
<li><code>1 &lt;= n &lt;= 10^4</code></li>
6347
</ul>
@@ -69,13 +53,51 @@ In the second operation choose x = 2 and y = 0 again, thus arr = [3, 3, 3].
6953
### **Python3**
7054

7155
```python
72-
56+
class Solution:
57+
def minOperations(self, n: int) -> int:
58+
ans = 0
59+
for i in range(n >> 1):
60+
ans += (n - (2 * i + 1))
61+
return ans
7362
```
7463

7564
### **Java**
7665

7766
```java
67+
class Solution {
68+
public int minOperations(int n) {
69+
int ans = 0;
70+
for (int i = 0; i < (n >> 1); i++) {
71+
ans += (n - (2 * i + 1));
72+
}
73+
return ans;
74+
}
75+
}
76+
```
77+
78+
### **C++**
79+
80+
```cpp
81+
class Solution {
82+
public:
83+
int minOperations(int n) {
84+
int ans = 0;
85+
for (int i = 0; i < (n >> 1); ++i) ans += (n - (2 * i + 1));
86+
return ans;
87+
}
88+
};
89+
```
90+
91+
### **Go**
7892
93+
```go
94+
func minOperations(n int) int {
95+
ans := 0
96+
for i := 0; i < (n >> 1); i++ {
97+
ans += (n - (2*i + 1))
98+
}
99+
return ans
100+
}
79101
```
80102

81103
### **...**
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
class Solution {
2+
public:
3+
int minOperations(int n) {
4+
int ans = 0;
5+
for (int i = 0; i < (n >> 1); ++i) ans += (n - (2 * i + 1));
6+
return ans;
7+
}
8+
};
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
func minOperations(n int) int {
2+
ans := 0
3+
for i := 0; i < (n >> 1); i++ {
4+
ans += (n - (2*i + 1))
5+
}
6+
return ans
7+
}

‎solution/1500-1599/1551.Minimum Operations to Make Array Equal/Solution.java‎

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
class Solution {
22
public int minOperations(int n) {
33
int ans = 0;
4-
for (int i = 0; i < n / 2; i++) {
5-
int curr = 2 * i + 1;
6-
ans += Math.abs(n - curr);
4+
for (int i = 0; i < (n >> 1); i++) {
5+
ans += (n - (2 * i + 1));
76
}
87
return ans;
98
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
class Solution:
2+
def minOperations(self, n: int) -> int:
3+
ans = 0
4+
for i in range(n >> 1):
5+
ans += (n - (2 * i + 1))
6+
return ans

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /