-
Notifications
You must be signed in to change notification settings - Fork 5
/
KthLargestElementInAStream.java
40 lines (35 loc) · 1.3 KB
/
KthLargestElementInAStream.java
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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
/*
Problem Statement:
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.
Problem Link:
Kth Largest Element In A Stream: https://leetcode.com/problems/kth-largest-element-in-a-stream/description/
Solution:
https://github.com/sunnypatel165/leetcode-again/blob/master/solutions/KthLargestElementInAStream.java
Author:
Sunny Patel
https://github.com/sunnypatel165
https://www.linkedin.com/in/sunnypatel165/
*/
class KthLargest {
final PriorityQueue<Integer> q;
final int k;
public KthLargest(int k, int[] a) {
this.k = k;
q = new PriorityQueue<>(k);
for (int n : a)
add(n);
}
public int add(int n) {
if (q.size() < k)
q.offer(n);
else if (q.peek() < n) {
q.poll();
q.offer(n);
}
return q.peek();
}
}