-
Notifications
You must be signed in to change notification settings - Fork 28
/
Copy pathTop_K_Frequent_Words.cpp
38 lines (38 loc) · 1.21 KB
/
Top_K_Frequent_Words.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
32
33
34
35
36
37
38
// O(nlogk) time and O(n) space
class Solution {
class Compare {
public:
bool operator() (pair<int, string> const& p, pair<int, string> const& q) {
if(p.first == q.first) {
return p.second < q.second;
}
return p.first > q.first;
}
};
public:
vector<string> topKFrequent(vector<string>& words, int k) {
int n = (int)words.size();
unordered_map<string, int> freq;
for(string& word: words) {
freq[word]++;
}
priority_queue<pair<int, string>, vector<pair<int, string>>, Compare> pQ;
for(auto entry = freq.begin(); entry != freq.end(); entry++) {
if(pQ.size() < k) {
pQ.push({entry->second, entry->first});
} else {
if((pQ.top().first < entry->second) or
(pQ.top().first == entry->second and entry->first < pQ.top().second)) {
pQ.pop();
pQ.push({entry->second, entry->first});
}
}
}
vector<string> result(k);
while(!pQ.empty()) {
result[--k] = pQ.top().second;
pQ.pop();
}
return result;
}
};