-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path692.cpp
31 lines (30 loc) · 825 Bytes
/
692.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
31
// priority_queue.cpp
class Solution {
struct cmp {
bool operator()(const pair<string, int> &a, const pair<string, int> &b) {
return a.second > b.second || (a.second == b.second && a.first < b.first);
}
};
public:
vector<string> topKFrequent(vector<string> &words, int k) {
unordered_map<string, int> hashmap;
for (auto &str : words)
++hashmap[str];
priority_queue<pair<string, int>, vector<pair<string, int>>, cmp> pq;
auto it = hashmap.begin();
for (int i = 0; i < k; ++i)
pq.push(*it++);
vector<string> res(k);
cmp compare;
for (; it != hashmap.end(); ++it)
if (compare(*it, pq.top())) {
pq.pop();
pq.push(*it);
}
for (int i = k - 1; i >= 0; i--) {
res[i] = pq.top().first;
pq.pop();
}
return res;
}
};