Problem description:

Design a class to find the kth largest element in a stream. Note that it is the kth largest element in the sorted order, not the kth distinct element.

Your KthLargest class will have a constructor which accepts an integer k and an integer array nums, which contains initial elements from the stream. For each call to the method KthLargest.add, return the element representing the kth largest element in the stream.

Example:

int k = 3;
int[] arr = [4,5,8,2];
KthLargest kthLargest = new KthLargest(3, arr);
kthLargest.add(3); // returns 4
kthLargest.add(5); // returns 5
kthLargest.add(10); // returns 5
kthLargest.add(9); // returns 8
kthLargest.add(4); // returns 8
Note:
You may assume that nums’ length ≥ k-1 and k ≥ 1.

Solution:

In the question, it asked us to return the kth largest element(not distinct element). To this kind of question, we can use priority_queue to solve.

Maintain a priority_queue, minheap, with size k.
Whenever we push an element into queue, check if the queue size exceed k.
If it exceed k, pop out the top element(the smallest one).

example:
minheap: [], k= 3

push 4 [4]
push 5 [4,5]
push 8 [4,5,8]
push 2 [2,4,5,8], since the size of queue is 4 exceed k, pop out the element at top.
[4,5,8]

push 3 [3,4,5,8]
[4,5,8]

push 5 [4,5,5,8]
[5,5,8]

push 10 [5,5,8,10]
[5,8,10]

1
2
3
4
5
6
7
8
9
10
11
12
13
14
class KthLargest:
def __init__(self, k: int, nums: List[int]):
self.pool = nums
self.k = k
heapify(self.pool)
while len(self.pool) > k:
heapq.heappop(self.pool)

def add(self, val: int) -> int:
if len(self.pool) < self.k:
heapq.heappush(self.pool, val)
elif val > self.pool[0]:
heapq.heapreplace(self.pool, val)
return self.pool[0]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
class KthLargest {
public:
priority_queue<int, vector<int>, greater<int>> pq;
int _k;
KthLargest(int k, vector<int> nums) {
_k= k;
for(auto n: nums){
pq.push(n);
if(pq.size()> _k)
pq.pop();
}
}

int add(int val) {
pq.push(val);
if(pq.size()> _k)
pq.pop();
return pq.top();
}
};

/**
* Your KthLargest object will be instantiated and called as such:
* KthLargest obj = new KthLargest(k, nums);
* int param_1 = obj.add(val);
*/

time complexity: $O(nlogn)$, because the push takes $O(logn)$
space complexity: $O(1)$, because only need to keep k elements
reference: