https://leetcode.com/problems/top-k-frequent-words/description/
class Solution {
public:
vector<string> topKFrequent(vector<string>& words, int k) {
unordered_map<string, int> freq;
for (auto & w : words)
freq[w]++;
priority_queue<pair<int,string>> q;
for (auto & f : freq)
{
q.push({-f.second, f.first});
if (q.size() > k)
q.pop();
}
vector<string> res;
while (!q.empty())
{
res.push_back(q.top().second);
q.pop();
}
reverse(res.begin(), res.end());
return res;
}
};