-
Notifications
You must be signed in to change notification settings - Fork 7
/
Main.java
39 lines (33 loc) · 786 Bytes
/
Main.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
package com.company;
import java.util.Comparator;
import java.util.PriorityQueue;
public class Main {
public static void main(String[] args) {
int k = 3;
int[] nums = {4, 5, 8, 2};
KthLargest obj = new KthLargest(k, nums);
obj.add(3);
obj.add(5);
obj.add(10);
obj.add(9);
obj.add(4);
}
}
class KthLargest {
PriorityQueue<Integer> queue;
int size;
public KthLargest(int k, int[] nums) {
queue = new PriorityQueue<>();
size = k;
for(int num : nums){
queue.offer(num);
if(queue.size() > k)
queue.poll();
}
}
public int add(int val) {
queue.offer(val);
if(queue.size() > size) queue.poll();
return queue.peek();
}
}