-
Notifications
You must be signed in to change notification settings - Fork 13
/
Heap_KthLargestElementInStream.java
60 lines (54 loc) · 2.07 KB
/
Heap_KthLargestElementInStream.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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import java.util.PriorityQueue;
import java.util.Queue;
// https://practice.geeksforgeeks.org/problems/kth-largest-element-in-a-stream/0
// https://leetcode.com/problems/kth-largest-element-in-a-stream/
// https://www.youtube.com/results?search_query=kth+largest+element+in+a+stream
public class Heap_KthLargestElementInStream {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int t = Integer.parseInt(br.readLine().trim());
while(t-->0) {
String[] kn = br.readLine().trim().split("\\s+");
int k = Integer.parseInt(kn[0]);
int n = Integer.parseInt(kn[1]);
String[] str = br.readLine().trim().split("\\s+");
int[] ip = new int[n];
for(int i = 0; i < n; ++i) {
ip[i] = Integer.parseInt(str[i]);
}
List<Integer> output = findKthLargestElementInStream(ip, k);
printOutput(output);
}
}
private static List<Integer> findKthLargestElementInStream(int[] ip, int k) {
List<Integer> result = new ArrayList<>();
Queue<Integer> minHeap = new PriorityQueue<>();
for(int num : ip) {
if(minHeap.size() < k) {
minHeap.offer(num);
}
else {
// if heap size is greater than k, then add it the heap if the
// upcoming value is greater than the top of heap.
if(minHeap.peek() < num) {
minHeap.poll();
minHeap.offer(num);
}
}
result.add(minHeap.size() < k ? -1 : minHeap.peek());
}
return result;
}
private static void printOutput(List<Integer> list) {
StringBuffer sb = new StringBuffer();
for(int num : list) {
sb.append(num + " ");
}
System.out.println(sb);
}
}