-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsolution.java
More file actions
35 lines (32 loc) · 863 Bytes
/
Copy pathsolution.java
File metadata and controls
35 lines (32 loc) · 863 Bytes
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
// 703. Kth Largest Element in a Stream
// https://leetcode.com/problems/kth-largest-element-in-a-stream/
// Easy | Java | Accepted 2025-11-29
// Runtime 22 ms | Memory 52 MB
class KthLargest {
private PriorityQueue<Integer> minHeap;
private int k;
public KthLargest(int k, int[] nums) {
this.k = k;
minHeap = new PriorityQueue<>();
for(int i = 0; i<nums.length; i++)
{
add(nums[i]);
}
}
public int add(int val) {
if(minHeap.size()<k || val>minHeap.peek())
{
minHeap.add(val);
}
if(minHeap.size()>k)
{
minHeap.remove();
}
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);
*/