image_2022-12-15_00-24-17.png
29.5 KB
Runtime 13 ms Beats 95.50%#N703. Kth Largest Element in a Stream
Memory 46.2 MB Beats 91.49%
problem link
#solution
class KthLargest {
int k;
PriorityQueue<Integer> pq;
public KthLargest(int k, int[] nums) {
pq=new PriorityQueue<>();
for(int n: nums) pq.offer(n);
this.k=k;
while(pq.size()>k) pq.poll();
}
public int add(int val) {
pq.offer(val);
if(pq.size()>k) pq.poll();
return pq.peek();
}
}⚡3👍1