C++ Tricks

  1. using v.emplace_back(i.second, i.first);
    instead of v.push_back(make_pair(i.second, i.first));
class Solution {
public:
int minSetSize(vector<int>& arr) {
unordered_map<int, int> m;
for (int i : arr)
m[i]++;
vector<pair<int, int>> v;
for (auto& i : m)
v.emplace_back(i.second, i.first);
//instead of v.push_back(make_pair(i.second, i.first));
sort(v.begin(), v.end());
reverse(v.begin(), v.end());
int ans = 0;
int tmp = 0;
for (auto i : v)
{
tmp += i.first * 2;
ans++;
if (tmp >= arr.size())break;
}
return ans;
}
};

Data Scientist/MLE/SWE @takemobi