class KthLargest {
public:
KthLargest(int k, vector<int>& nums) {
}
int add(int val) {
}
};
/**
* Your KthLargest object will be instantiated and called as such:
* KthLargest* obj = new KthLargest(k, nums);
* int param_1 = obj->add(val);
*/
class KthLargest {
private Queue<Integer> minHeap;
private int k;
public KthLargest(int k, int[] nums) {
minHeap = new PriorityQueue<>(k); //只维护K个元素,堆顶就是第K小
this.k = k;
for (int i : nums)
add(i);
}
// 满了弹出去最小的
public int add(int val) {
if ( minHeap.size() < this.k ) {
minHeap.add(val);
} else {
if ( val > minHeap.peek() ) {
minHeap.poll();
minHeap.add(val);
}
}
return minHeap.peek();
}
}
/**
* Your KthLargest object will be instantiated and called as such:
* KthLargest obj = new KthLargest(k, nums);
* int param_1 = obj.add(val);
*/