-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtopKFrequent.cpp
More file actions
27 lines (27 loc) · 869 Bytes
/
topKFrequent.cpp
File metadata and controls
27 lines (27 loc) · 869 Bytes
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
class Solution {
public:
using pp = unordered_map<string, int>::iterator;
vector<string> topKFrequent(vector<string>& words, int k) {
function<bool(pp &a, pp &b)> cmp = [](pp &a, pp &b) -> bool {
return a->second == b->second ? a->first < b->first : a->second > b->second;
};
priority_queue<pp, vector<pp>, function<bool(pp &a, pp &b)> > pq(cmp);
unordered_map<string, int> m;
for (string &w : words) {
++m[w];
}
for (auto it = m.begin(); it != m.end(); ++it) {
pq.push(it);
if (pq.size() > k)
pq.pop();
}
vector<string> ans;
ans.reserve(k);
while (!pq.empty()) {
ans.push_back(pq.top()->first);
pq.pop();
}
reverse(ans.begin(), ans.end());
return ans;
}
};