mirror of
https://github.com/lWolvesl/leetcode.git
synced 2026-01-12 18:08:38 +08:00
routine
This commit is contained in:
35
25/12/comp/20q1.cpp
Normal file
35
25/12/comp/20q1.cpp
Normal file
@@ -0,0 +1,35 @@
|
||||
#include <unordered_map>
|
||||
|
||||
class Solution {
|
||||
public:
|
||||
int minOperations(std::vector<int>& nums) {
|
||||
std::unordered_map<int, int> m;
|
||||
int count = 0;
|
||||
for(auto num: nums){
|
||||
m[num] ++;
|
||||
if (m[num] > 1) {
|
||||
count++;
|
||||
}
|
||||
}
|
||||
if(count == 0){
|
||||
return 0;
|
||||
}
|
||||
int ans = 0;
|
||||
for (int i = 0; i<nums.size(); i++) {
|
||||
if (m[nums[i]] > 1) {
|
||||
m[nums[i]]--;
|
||||
count--;
|
||||
}
|
||||
if ((i + 1) % 3 == 0) {
|
||||
ans++;
|
||||
}
|
||||
if (count == 0) {
|
||||
if ((i + 1) % 3 != 0) {
|
||||
ans++;
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
return ans;
|
||||
}
|
||||
};
|
||||
Reference in New Issue
Block a user