-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path_0703.java
More file actions
34 lines (29 loc) · 869 Bytes
/
_0703.java
File metadata and controls
34 lines (29 loc) · 869 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
package com.github.aditya;
import java.util.PriorityQueue;
public class _0703 {
// 10 ms, faster than 99.87%, memory 45.2 MB, less than 97.86%
class KthLargest {
final PriorityQueue<Integer> minHeap;
final int k;
public KthLargest(int k, int[] nums) {
this.k = k;
minHeap = new PriorityQueue<>(k + 1);
for (int num : nums)
add(num);
}
public int add(int val) {
if (minHeap.size() < k)
minHeap.offer(val);
else if (minHeap.peek() < val) {
minHeap.poll();
minHeap.offer(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);
*/
}