Skip to content

Commit e9cfffd

Browse files
authored
feat: add solutions to lc problem: No.2870 (#1732)
No.2870.Minimum Number of Operations to Make Array Empty
1 parent 675a1f8 commit e9cfffd

File tree

7 files changed

+174
-11
lines changed

7 files changed

+174
-11
lines changed

solution/2800-2899/2870.Minimum Number of Operations to Make Array Empty/README.md

+60-5
Original file line numberDiff line numberDiff line change
@@ -53,14 +53,28 @@
5353

5454
<!-- 这里可写通用的实现逻辑 -->
5555

56+
**方法一:哈希表 + 贪心**
57+
58+
我们用一个哈希表 $count$ 统计数组中每个元素出现的次数,然后遍历哈希表,对于每个元素 $x$,如果 $x$ 出现的次数为 $c$,那么我们可以进行 $\lfloor \frac{c+2}{3} \rfloor$ 次操作,将 $x$ 删除,最后我们返回所有元素的操作次数之和即可。
59+
60+
时间复杂度 $O(n)$,空间复杂度 $O(n)$。其中 $n$ 是数组的长度。
61+
5662
<!-- tabs:start -->
5763

5864
### **Python3**
5965

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

6268
```python
63-
69+
class Solution:
70+
def minOperations(self, nums: List[int]) -> int:
71+
count = Counter(nums)
72+
ans = 0
73+
for c in count.values():
74+
if c == 1:
75+
return -1
76+
ans += (c + 2) // 3
77+
return ans
6478
```
6579

6680
### **Java**
@@ -76,7 +90,7 @@ class Solution {
7690
count.merge(num, 1, Integer::sum);
7791
}
7892
int ans = 0;
79-
for (Integer c : count.values()) {
93+
for (int c : count.values()) {
8094
if (c < 2) {
8195
return -1;
8296
}
@@ -99,19 +113,60 @@ class Solution {
99113
### **C++**
100114

101115
```cpp
102-
116+
class Solution {
117+
public:
118+
int minOperations(vector<int>& nums) {
119+
unordered_map<int, int> count;
120+
for (int num : nums) {
121+
++count[num];
122+
}
123+
int ans = 0;
124+
for (auto& [_, c] : count) {
125+
if (c < 2) {
126+
return -1;
127+
}
128+
ans += (c + 2) / 3;
129+
}
130+
return ans;
131+
}
132+
};
103133
```
104134
105135
### **Go**
106136
107137
```go
108-
138+
func minOperations(nums []int) (ans int) {
139+
count := map[int]int{}
140+
for _, num := range nums {
141+
count[num]++
142+
}
143+
for _, c := range count {
144+
if c < 2 {
145+
return -1
146+
}
147+
ans += (c + 2) / 3
148+
}
149+
return
150+
}
109151
```
110152

111153
### **TypeScript**
112154

113155
```ts
114-
156+
function minOperations(nums: number[]): number {
157+
const count: Map<number, number> = new Map();
158+
for (const num of nums) {
159+
count.set(num, (count.get(num) ?? 0) + 1);
160+
}
161+
let ans = 0;
162+
for (const [_, c] of count) {
163+
if (c < 2) {
164+
return -1;
165+
}
166+
ans += ((c + 2) / 3) | 0;
167+
}
168+
return ans;
169+
}
115170
```
116171

117172
### **...**

solution/2800-2899/2870.Minimum Number of Operations to Make Array Empty/README_EN.md

+60-5
Original file line numberDiff line numberDiff line change
@@ -47,12 +47,26 @@ It can be shown that we cannot make the array empty in less than 4 operations.
4747

4848
## Solutions
4949

50+
**Solution 1: Hash Table + Greedy**
51+
52+
We use a hash table $count$ to count the number of occurrences of each element in the array. Then we traverse the hash table. For each element $x$, if it appears $c$ times, we can perform $\lfloor \frac{c+2}{3} \rfloor$ operations to delete $x$. Finally, we return the sum of the number of operations for all elements.
53+
54+
The time complexity is $O(n)$, where $n$ is the length of the array. The space complexity is $O(n)$.
55+
5056
<!-- tabs:start -->
5157

5258
### **Python3**
5359

5460
```python
55-
61+
class Solution:
62+
def minOperations(self, nums: List[int]) -> int:
63+
count = Counter(nums)
64+
ans = 0
65+
for c in count.values():
66+
if c == 1:
67+
return -1
68+
ans += (c + 2) // 3
69+
return ans
5670
```
5771

5872
### **Java**
@@ -66,7 +80,7 @@ class Solution {
6680
count.merge(num, 1, Integer::sum);
6781
}
6882
int ans = 0;
69-
for (Integer c : count.values()) {
83+
for (int c : count.values()) {
7084
if (c < 2) {
7185
return -1;
7286
}
@@ -89,19 +103,60 @@ class Solution {
89103
### **C++**
90104

91105
```cpp
92-
106+
class Solution {
107+
public:
108+
int minOperations(vector<int>& nums) {
109+
unordered_map<int, int> count;
110+
for (int num : nums) {
111+
++count[num];
112+
}
113+
int ans = 0;
114+
for (auto& [_, c] : count) {
115+
if (c < 2) {
116+
return -1;
117+
}
118+
ans += (c + 2) / 3;
119+
}
120+
return ans;
121+
}
122+
};
93123
```
94124
95125
### **Go**
96126
97127
```go
98-
128+
func minOperations(nums []int) (ans int) {
129+
count := map[int]int{}
130+
for _, num := range nums {
131+
count[num]++
132+
}
133+
for _, c := range count {
134+
if c < 2 {
135+
return -1
136+
}
137+
ans += (c + 2) / 3
138+
}
139+
return
140+
}
99141
```
100142

101143
### **TypeScript**
102144

103145
```ts
104-
146+
function minOperations(nums: number[]): number {
147+
const count: Map<number, number> = new Map();
148+
for (const num of nums) {
149+
count.set(num, (count.get(num) ?? 0) + 1);
150+
}
151+
let ans = 0;
152+
for (const [_, c] of count) {
153+
if (c < 2) {
154+
return -1;
155+
}
156+
ans += ((c + 2) / 3) | 0;
157+
}
158+
return ans;
159+
}
105160
```
106161

107162
### **...**
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
class Solution {
2+
public:
3+
int minOperations(vector<int>& nums) {
4+
unordered_map<int, int> count;
5+
for (int num : nums) {
6+
++count[num];
7+
}
8+
int ans = 0;
9+
for (auto& [_, c] : count) {
10+
if (c < 2) {
11+
return -1;
12+
}
13+
ans += (c + 2) / 3;
14+
}
15+
return ans;
16+
}
17+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
func minOperations(nums []int) (ans int) {
2+
count := map[int]int{}
3+
for _, num := range nums {
4+
count[num]++
5+
}
6+
for _, c := range count {
7+
if c < 2 {
8+
return -1
9+
}
10+
ans += (c + 2) / 3
11+
}
12+
return
13+
}

solution/2800-2899/2870.Minimum Number of Operations to Make Array Empty/Solution.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ public int minOperations(int[] nums) {
66
count.merge(num, 1, Integer::sum);
77
}
88
int ans = 0;
9-
for (Integer c : count.values()) {
9+
for (int c : count.values()) {
1010
if (c < 2) {
1111
return -1;
1212
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
class Solution:
2+
def minOperations(self, nums: List[int]) -> int:
3+
count = Counter(nums)
4+
ans = 0
5+
for c in count.values():
6+
if c == 1:
7+
return -1
8+
ans += (c + 2) // 3
9+
return ans
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
function minOperations(nums: number[]): number {
2+
const count: Map<number, number> = new Map();
3+
for (const num of nums) {
4+
count.set(num, (count.get(num) ?? 0) + 1);
5+
}
6+
let ans = 0;
7+
for (const [_, c] of count) {
8+
if (c < 2) {
9+
return -1;
10+
}
11+
ans += ((c + 2) / 3) | 0;
12+
}
13+
return ans;
14+
}

0 commit comments

Comments
 (0)