题目描述
解法:优先队列
C++ 实现
class KthLargest {
private:
int k;
priority_queue<int, vector<int>, greater<int>> q;
public:
KthLargest(int k, vector<int>& nums) {
this->k = k;
for(auto num: nums) add(num);
}
int add(int val) {
q.push(val);
if(q.size()>k) q.pop();
return q.top();
}
};
/**
* Your KthLargest object will be instantiated and called as such:
* KthLargest* obj = new KthLargest(k, nums);
* int param_1 = obj->add(val);
*/