-
Notifications
You must be signed in to change notification settings - Fork 0
/
KLargestElements.java
35 lines (29 loc) · 1019 Bytes
/
KLargestElements.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
package priorityqueue;
import java.util.PriorityQueue;
public class KLargestElements {
public static void main(String[] args) {
// o/p : 18 17, 7
// space complexity k
// TC : n log k
int[] arr = {2, 10, 5, 17, 7, 18, 6, 4};
int k = 3;
// create a PQ with priority given to minimum no.
// Why? We want to give highest priority to a number which is the weakest
// because that number will be dropped if some better candidate is there
// Golden rule: to get into the team you just need to be better than their worst player!!!
PriorityQueue<Integer> pq = new PriorityQueue<>();
for (int i=0; i<k; i++) {
pq.add(arr[i]);
}
for (int i=k; i<arr.length; i++) {
if (arr[i] > pq.peek()) {
pq.remove();
pq.add(arr[i]);
}
}
while (pq.size()>0) {
System.out.println(pq.peek());
pq.remove();
}
}
}