703. Kth Largest Element in a Stream

problem

solution

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
class KthLargest {
private:
priority_queue<int, vector<int>, greater<int>> pq;
int size ;
public:
KthLargest(int k, vector<int>& nums) {
size = k;
for(int n:nums){
pq.push(n);
if(pq.size() > size) pq.pop();
}
}
int add(int val) {
pq.push(val);
if(pq.size() > size) pq.pop();
return pq.top();
}
};

  • 實現BST