cpp: add “2870. Minimum Number of Operations to Make Array Empty”
Signed-off-by: Matej Focko <mfocko@redhat.com>
This commit is contained in:
parent
6d44469c3c
commit
489d931f11
1 changed files with 32 additions and 0 deletions
32
cpp/minimum-number-of-operations-to-make-array-empty.cpp
Normal file
32
cpp/minimum-number-of-operations-to-make-array-empty.cpp
Normal 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;
|
||||
}
|
||||
};
|
Loading…
Reference in a new issue