Created
July 22, 2020 09:02
-
-
Save JyotinderSingh/93f2e1202456198ea7cb1e20e7f36ece to your computer and use it in GitHub Desktop.
Reduce Array Size to the Half (LeetCode) | Interview Question Explained
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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