-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpriorityqueue.cpp
More file actions
61 lines (56 loc) · 1.49 KB
/
Copy pathpriorityqueue.cpp
File metadata and controls
61 lines (56 loc) · 1.49 KB
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
/*
cmp need to reverse for the priority queue
*/
typedef pair<int, string> PSI;
class Solution {
public:
// need to reverse from the normal style of writing
struct cmp {
bool operator()(const PSI& a, const PSI& b) const {
if (a.first == b.first) return a.second > b.second;
else return a.first < b.first;
}
};
vector<string> topKFrequent(vector<string>& words, int k) {
unordered_map<string, int> hash;
for (auto& s : words) hash[s]++;
priority_queue<PSI, vector<PSI>, cmp> pq;
for (auto [k, v] : hash) {
pq.push({v, k});
}
vector<string> res;
for (int i = 0; i < k; i++) {
res.push_back(pq.top().second);
pq.pop();
}
return res;
}
};
/*
Two pq to maintain median
*/
class Median {
priority_queue<int, vector<int>, greater<int>> up;
priority_queue<int> down;
void add(int num) {
if (down.empty() || num <= down.top()) {
down.push(num);
if (down.size() > up.size() + 1) {
up.push(down.top());
down.pop();
}
} else {
up.push(num);
if (up.size() > down.size()) {
down.push(up.top());
up.pop();
}
}
}
double getMedian() {
if ((up.size() + down.size()) & 1)
return up.top();
else
return (up.top() + down.top()) / 2.0;
}
}