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 fdff3c3

Browse files
feat: add solutions to lc problem: No.1887 (#4051)
No.1887.Reduction Operations to Make the Array Elements Equal
1 parent 62334f8 commit fdff3c3

File tree

13 files changed

+106
-207
lines changed

13 files changed

+106
-207
lines changed

‎solution/1800-1899/1887.Reduction Operations to Make the Array Elements Equal/README.md‎

Lines changed: 37 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,8 @@ tags:
5757
<strong>输出:</strong>4
5858
<strong>解释:</strong>需要 4 次操作使 nums 中的所有元素相等:
5959
1. largest = 3 下标为 4 。nextLargest = 2 。将 nums[4] 减少到 2 。nums = [1,1,2,2,<strong>2</strong>] 。
60-
2. largest = 2 下标为 2 。nextLargest = 1 。将 nums[2] 减少到 1 。nums = [1,1,<strong>1</strong>,2,2] 。
61-
3. largest = 2 下标为 3 。nextLargest = 1 。将 nums[3] 减少到 1 。nums = [1,1,1,<strong>1</strong>,2] 。
60+
2. largest = 2 下标为 2 。nextLargest = 1 。将 nums[2] 减少到 1 。nums = [1,1,<strong>1</strong>,2,2] 。
61+
3. largest = 2 下标为 3 。nextLargest = 1 。将 nums[3] 减少到 1 。nums = [1,1,1,<strong>1</strong>,2] 。
6262
4. largest = 2 下标为 4 。nextLargest = 1 。将 nums[4] 减少到 1 。nums = [1,1,1,1,<strong>1</strong>] 。
6363
</pre>
6464

@@ -79,11 +79,9 @@ tags:
7979

8080
### 方法一:排序
8181

82-
对 $nums$ 进行排序,用 $cnt$ 表示元素所需的操作次数,初始时 $cnt=0$
82+
我们首先对数组 $\textit{nums}$ 进行排序,然后从数组的第二个元素开始遍历,如果当前元素和前一个元素不相等,那么我们就将 $\textit{cnt}$ 加一,表示我们需要将当前元素减小到最小值的操作次数。然后我们将 $\textit{ans}$ 加上 $\textit{cnt},ドル继续遍历下一个元素
8383

84-
遍历 $nums[1..n-1],ドル如果当前元素 $nums[i]$ 不等于 $nums[i-1],ドル则将 $cnt$ 加一。累加当前 $cnt$ 到答案 $ans$。
85-
86-
时间复杂度 $O(nlogn)$。
84+
时间复杂度 $O(n \times \log n),ドル空间复杂度 $O(\log n)$。其中 $n$ 是数组 $\textit{nums}$ 的长度。
8785

8886
<!-- tabs:start -->
8987

@@ -94,8 +92,8 @@ class Solution:
9492
def reductionOperations(self, nums: List[int]) -> int:
9593
nums.sort()
9694
ans = cnt = 0
97-
for i, v in enumerate(nums[1:]):
98-
if v != nums[i]:
95+
for a, b in pairwise(nums):
96+
if a != b:
9997
cnt += 1
10098
ans += cnt
10199
return ans
@@ -125,7 +123,7 @@ class Solution {
125123
class Solution {
126124
public:
127125
int reductionOperations(vector<int>& nums) {
128-
sort(nums.begin(), nums.end());
126+
ranges::sort(nums);
129127
int ans = 0, cnt = 0;
130128
for (int i = 1; i < nums.size(); ++i) {
131129
cnt += nums[i] != nums[i - 1];
@@ -139,16 +137,16 @@ public:
139137
#### Go
140138
141139
```go
142-
func reductionOperations(nums []int) int {
140+
func reductionOperations(nums []int) (ans int) {
143141
sort.Ints(nums)
144-
ans, cnt := 0, 0
145-
for i, v := range nums[1:] {
146-
if v != nums[i] {
142+
cnt := 0
143+
for i, x := range nums[1:] {
144+
if x != nums[i] {
147145
cnt++
148146
}
149147
ans += cnt
150148
}
151-
return ans
149+
return
152150
}
153151
```
154152

@@ -157,10 +155,9 @@ func reductionOperations(nums []int) int {
157155
```ts
158156
function reductionOperations(nums: number[]): number {
159157
nums.sort((a, b) => a - b);
160-
let ans = 0;
161-
let cnt = 0;
158+
let [ans, cnt] = [0, 0];
162159
for (let i = 1; i < nums.length; ++i) {
163-
if (nums[i] != nums[i - 1]) {
160+
if (nums[i] !== nums[i - 1]) {
164161
++cnt;
165162
}
166163
ans += cnt;
@@ -169,81 +166,43 @@ function reductionOperations(nums: number[]): number {
169166
}
170167
```
171168

169+
#### JavaScript
170+
171+
```js
172+
/**
173+
* @param {number[]} nums
174+
* @return {number}
175+
*/
176+
var reductionOperations = function (nums) {
177+
nums.sort((a, b) => a - b);
178+
let [ans, cnt] = [0, 0];
179+
for (let i = 1; i < nums.length; ++i) {
180+
if (nums[i] !== nums[i - 1]) {
181+
++cnt;
182+
}
183+
ans += cnt;
184+
}
185+
return ans;
186+
};
187+
```
188+
172189
#### C#
173190

174191
```cs
175192
public class Solution {
176193
public int ReductionOperations(int[] nums) {
177194
Array.Sort(nums);
178-
int ans = 0, up = 0;
195+
int ans = 0, cnt = 0;
179196
for (int i = 1; i < nums.Length; i++) {
180197
if (nums[i] != nums[i - 1]) {
181-
up++;
198+
++cnt;
182199
}
183-
ans += up;
184-
}
185-
return ans;
186-
}
187-
}
188-
```
189-
190-
<!-- tabs:end -->
191-
192-
<!-- solution:end -->
193-
194-
<!-- solution:start -->
195-
196-
### 方法二
197-
198-
<!-- tabs:start -->
199-
200-
#### Python3
201-
202-
```python
203-
class Solution:
204-
def reductionOperations(self, nums: List[int]) -> int:
205-
ans = cnt = 0
206-
for _, v in sorted(Counter(nums).items()):
207-
ans += cnt * v
208-
cnt += 1
209-
return ans
210-
```
211-
212-
#### Java
213-
214-
```java
215-
class Solution {
216-
public int reductionOperations(int[] nums) {
217-
Map<Integer, Integer> tm = new TreeMap<>();
218-
for (int v : nums) {
219-
tm.put(v, tm.getOrDefault(v, 0) + 1);
220-
}
221-
int ans = 0, cnt = 0;
222-
for (int v : tm.values()) {
223-
ans += cnt * v;
224-
++cnt;
200+
ans += cnt;
225201
}
226202
return ans;
227203
}
228204
}
229-
```
230-
231-
#### C++
232205

233-
```cpp
234-
class Solution {
235-
public:
236-
int reductionOperations(vector<int>& nums) {
237-
map<int, int> m;
238-
for (int v : nums) ++m[v];
239-
int ans = 0, cnt = 0;
240-
for (auto [_, v] : m) {
241-
ans += cnt * v;
242-
++cnt;
243-
}
244-
return ans;
245-
}
246-
};
247206
```
248207

249208
<!-- tabs:end -->

‎solution/1800-1899/1887.Reduction Operations to Make the Array Elements Equal/README_EN.md‎

Lines changed: 38 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,11 @@ tags:
7575

7676
<!-- solution:start -->
7777

78-
### Solution 1
78+
### Solution 1: Sorting
79+
80+
We first sort the array $\textit{nums},ドル then iterate from the second element of the array. If the current element is not equal to the previous element, we increment $\textit{cnt},ドル indicating the number of operations needed to reduce the current element to the minimum value. Then we add $\textit{cnt}$ to $\textit{ans}$ and continue to the next element.
81+
82+
The time complexity is $O(n \times \log n),ドル and the space complexity is $O(\log n)$. Here, $n$ is the length of the array $\textit{nums}$.
7983

8084
<!-- tabs:start -->
8185

@@ -86,8 +90,8 @@ class Solution:
8690
def reductionOperations(self, nums: List[int]) -> int:
8791
nums.sort()
8892
ans = cnt = 0
89-
for i, v in enumerate(nums[1:]):
90-
if v != nums[i]:
93+
for a, b in pairwise(nums):
94+
if a != b:
9195
cnt += 1
9296
ans += cnt
9397
return ans
@@ -117,7 +121,7 @@ class Solution {
117121
class Solution {
118122
public:
119123
int reductionOperations(vector<int>& nums) {
120-
sort(nums.begin(), nums.end());
124+
ranges::sort(nums);
121125
int ans = 0, cnt = 0;
122126
for (int i = 1; i < nums.size(); ++i) {
123127
cnt += nums[i] != nums[i - 1];
@@ -131,16 +135,16 @@ public:
131135
#### Go
132136
133137
```go
134-
func reductionOperations(nums []int) int {
138+
func reductionOperations(nums []int) (ans int) {
135139
sort.Ints(nums)
136-
ans, cnt := 0, 0
137-
for i, v := range nums[1:] {
138-
if v != nums[i] {
140+
cnt := 0
141+
for i, x := range nums[1:] {
142+
if x != nums[i] {
139143
cnt++
140144
}
141145
ans += cnt
142146
}
143-
return ans
147+
return
144148
}
145149
```
146150

@@ -149,10 +153,9 @@ func reductionOperations(nums []int) int {
149153
```ts
150154
function reductionOperations(nums: number[]): number {
151155
nums.sort((a, b) => a - b);
152-
let ans = 0;
153-
let cnt = 0;
156+
let [ans, cnt] = [0, 0];
154157
for (let i = 1; i < nums.length; ++i) {
155-
if (nums[i] != nums[i - 1]) {
158+
if (nums[i] !== nums[i - 1]) {
156159
++cnt;
157160
}
158161
ans += cnt;
@@ -161,83 +164,44 @@ function reductionOperations(nums: number[]): number {
161164
}
162165
```
163166

167+
#### JavaScript
168+
169+
```js
170+
/**
171+
* @param {number[]} nums
172+
* @return {number}
173+
*/
174+
var reductionOperations = function (nums) {
175+
nums.sort((a, b) => a - b);
176+
let [ans, cnt] = [0, 0];
177+
for (let i = 1; i < nums.length; ++i) {
178+
if (nums[i] !== nums[i - 1]) {
179+
++cnt;
180+
}
181+
ans += cnt;
182+
}
183+
return ans;
184+
};
185+
```
186+
164187
#### C#
165188

166189
```cs
167190
public class Solution {
168191
public int ReductionOperations(int[] nums) {
169192
Array.Sort(nums);
170-
int ans = 0, up = 0;
193+
int ans = 0, cnt = 0;
171194
for (int i = 1; i < nums.Length; i++) {
172195
if (nums[i] != nums[i - 1]) {
173-
up++;
196+
++cnt;
174197
}
175-
ans += up;
176-
}
177-
return ans;
178-
}
179-
}
180-
```
181-
182-
<!-- tabs:end -->
183-
184-
<!-- solution:end -->
185-
186-
<!-- solution:start -->
187-
188-
### Solution 2
189-
190-
<!-- tabs:start -->
191-
192-
#### Python3
193-
194-
```python
195-
class Solution:
196-
def reductionOperations(self, nums: List[int]) -> int:
197-
ans = cnt = 0
198-
for _, v in sorted(Counter(nums).items()):
199-
ans += cnt * v
200-
cnt += 1
201-
return ans
202-
```
203-
204-
#### Java
205-
206-
```java
207-
class Solution {
208-
public int reductionOperations(int[] nums) {
209-
Map<Integer, Integer> tm = new TreeMap<>();
210-
for (int v : nums) {
211-
tm.put(v, tm.getOrDefault(v, 0) + 1);
212-
}
213-
int ans = 0, cnt = 0;
214-
for (int v : tm.values()) {
215-
ans += cnt * v;
216-
++cnt;
198+
ans += cnt;
217199
}
218200
return ans;
219201
}
220202
}
221203
```
222204

223-
#### C++
224-
225-
```cpp
226-
class Solution {
227-
public:
228-
int reductionOperations(vector<int>& nums) {
229-
map<int, int> m;
230-
for (int v : nums) ++m[v];
231-
int ans = 0, cnt = 0;
232-
for (auto [_, v] : m) {
233-
ans += cnt * v;
234-
++cnt;
235-
}
236-
return ans;
237-
}
238-
};
239-
```
240-
241205
<!-- tabs:end -->
242206

243207
<!-- solution:end -->
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
class Solution {
22
public:
33
int reductionOperations(vector<int>& nums) {
4-
sort(nums.begin(), nums.end());
4+
ranges::sort(nums);
55
int ans = 0, cnt = 0;
66
for (int i = 1; i < nums.size(); ++i) {
77
cnt += nums[i] != nums[i - 1];
88
ans += cnt;
99
}
1010
return ans;
1111
}
12-
};
12+
};

‎solution/1800-1899/1887.Reduction Operations to Make the Array Elements Equal/Solution.cs‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
public class Solution {
22
public int ReductionOperations(int[] nums) {
33
Array.Sort(nums);
4-
int ans = 0, up = 0;
4+
int ans = 0, cnt = 0;
55
for (int i = 1; i < nums.Length; i++) {
66
if (nums[i] != nums[i - 1]) {
7-
up++;
7+
++cnt;
88
}
9-
ans += up;
9+
ans += cnt;
1010
}
1111
return ans;
1212
}

0 commit comments

Comments
(0)

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