维护一个K个元素的最小堆
class KthLargest {
public:
KthLargest(int k, vector<int>& nums) {
// 维护一个k个元素的小顶堆
size = k;
for(auto num:nums){
q.push(num);
}
while(q.size()>k){
q.pop();
}
}
int add(int val) {
q.push(val);
if(q.size()>size){
q.pop();
}
return q.top();
}
priority_queue<int,vector<int>,greater<int> > q;
int size;
};
/**
* Your KthLargest object will be instantiated and called as such:
* KthLargest* obj = new KthLargest(k, nums);
* int param_1 = obj->add(val);
*/