We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent b02f5a4 commit f96e107Copy full SHA for f96e107
java/Sort and Search/KthLargestIntegerMinHeap.java
@@ -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
16
17
18
+ return minHeap.peek();
19
20
+}
0 commit comments