1
0
Fork 0
mirror of https://gitlab.com/mfocko/LeetCode.git synced 2024-11-14 18:07:32 +01:00
LeetCode/cpp/minimum-number-of-operations-to-make-array-empty.cpp

33 lines
655 B
C++
Raw Normal View History

#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;
}
};