Skip to content

Commit f96e107

Browse files
committed
add: KthLargestIntegerQuickSelect
1 parent b02f5a4 commit f96e107

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import java.util.PriorityQueue;
2+
3+
public class KthLargestIntegerMinHeap {
4+
public int kthLargestIntegerMinHeap(int[] nums, int k) {
5+
PriorityQueue<Integer> minHeap = new PriorityQueue<>();
6+
for (int num : nums) {
7+
// Ensure the heap has at least 'k' integers.
8+
if (minHeap.size() < k) {
9+
minHeap.offer(num);
10+
}
11+
// If 'num' is greater than the smallest integer in the heap, pop
12+
// off this smallest integer from the heap and push in 'num'.
13+
else if (num > minHeap.peek()) {
14+
minHeap.poll();
15+
minHeap.offer(num);
16+
}
17+
}
18+
return minHeap.peek();
19+
}
20+
}

0 commit comments

Comments
 (0)