mirror of
https://gitlab.com/mfocko/LeetCode.git
synced 2024-11-10 00:09:06 +01:00
32 lines
655 B
C++
32 lines
655 B
C++
#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;
|
|
}
|
|
};
|