-
Notifications
You must be signed in to change notification settings - Fork 0
/
largest-k-int.cpp
63 lines (58 loc) · 1.37 KB
/
largest-k-int.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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
/* Given a stream of integers, find the top k integers. 2 variations: 1)duplicates are allowed in the result, 2) duplicates are not allowed.
* Eg., Stream: 3,3,3,1,1,1,2,2,2,... Top 3 elements in (1) 3,3,3. Top 3 elements in (2) 3,2,1.
*/
#include<iostream>
using namespace std;
//duplicates allowed in result
template<class T>
class TopK1{
priority_queue<T, vector<T>, std::greater()> min_heap;
int k;
public:
void add(T a){
if(k <= 0) {
return;
}
if(min_heap.size() >= k && a < min_heap.top()){
return;
}
if(min_heap.push(a) && min_heap.size() > k) {
min_heap.pop();
}
}
vector<T> getK(){
vector<T> res;
while(!min_heap.empty()){
res.push_back(min_heap.top());
min_heap.pop();
}
return res;
}
};
//duplicates not allowed in result
template<class T>
class TopK2{
set<T> s;
int k;
public:
void add(T a){
if(k <=0 ) return;
if(s.size() >= k && a < *(s.begin())){
return;
}
if(s.insert(a) && s.size() > k) {
s.erase(s.begin());
}
}
vector<T> getK(){
vector<T> res;
for(auto i = s.begin(); i != s.end(); i++) {
res.push_back(*i);
}
return res;
}
};
/* Not tested.
* Time complexity: O(n log k) in both cases assuming set is implemented as a bst or similar.
* Space complexity: O(k)
*/