Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 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>> pq;
- for(auto w: freq){
- pq.push({-w.second, w.first});
- if(pq.size()>k) pq.pop();
- }
- vector<string> res;
- while(!pq.empty()){
- res.push_back(pq.top().second);
- pq.pop();
- }
- reverse(res.begin(), res.end());
- return res;
- }
- };
Advertisement
Add Comment
Please, Sign In to add comment