1
0
Fork 0
mirror of https://gitlab.com/mfocko/LeetCode.git synced 2024-11-10 00:09:06 +01:00

cpp: add “2870. Minimum Number of Operations to Make Array Empty”

Signed-off-by: Matej Focko <mfocko@redhat.com>
This commit is contained in:
Matej Focko 2024-01-04 17:52:29 +01:00
parent 6d44469c3c
commit 489d931f11
Signed by: mfocko
GPG key ID: 7C47D46246790496

View file

@ -0,0 +1,32 @@
#include <map>
#include <vector>
class Solution {
public:
int minOperations(const std::vector<int> &nums) {
std::map<int, int> freqs;
for (const auto &x : nums) {
++freqs[x];
}
int operations = 0;
for (const auto &[_, count] : freqs) {
if (count == 1) {
return -1;
}
switch (count % 3) {
case 0:
operations += count / 3;
break;
case 1:
case 2:
operations += count / 3 + 1;
break;
}
}
return operations;
}
};