-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathsolution.cpp
30 lines (30 loc) · 871 Bytes
/
solution.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
/**
* 110 / 110 test cases passed.
* Runtime: 16 ms
* Memory Usage: 10.2 MB
*/
class Solution {
public:
vector<string> topKFrequent(vector<string>& words, int k) {
unordered_map<string, int> freq;
for (auto& word: words) {
freq[word] += 1;
}
auto cmp = [](const pair<string, int>& a, const pair<string, int>& b) {
return a.second == b.second ? a.first < b.first : a.second > b.second;
};
priority_queue<pair<string, int>, vector<pair<string, int>>, decltype(cmp)> pq(cmp);
for (auto& it: freq) {
pq.emplace( it );
if (pq.size() > k) {
pq.pop();
}
}
vector<string> ans(k);
for (int i = k - 1; i >= 0; -- i) {
ans[i] = pq.top().first;
pq.pop();
}
return ans;
}
};