Skip to content

Instantly share code, notes, and snippets.

@JyotinderSingh
Created July 22, 2020 09:02
Show Gist options
  • Save JyotinderSingh/93f2e1202456198ea7cb1e20e7f36ece to your computer and use it in GitHub Desktop.
Save JyotinderSingh/93f2e1202456198ea7cb1e20e7f36ece to your computer and use it in GitHub Desktop.
Reduce Array Size to the Half (LeetCode) | Interview Question Explained
class Solution {
public:
int minSetSize(vector<int>& arr) {
unordered_map<int, int> freq;
for(int i = 0; i < arr.size(); ++i) freq[arr[i]]++;
priority_queue<int> pq;
for(auto iter: freq) pq.push(iter.second);
int count = 0, res = 0;
while(count * 2 < arr.size()) {
res++;
count += pq.top();
pq.pop();
}
return res;
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment