Skip to content

Commit 4577b69

Browse files
committed
One-Hundred-Sixty-Six Commit: Add Kth Largest Number in a Stream problem to Top K Elements section
1 parent 2519a61 commit 4577b69

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package Top_K_Elements;
2+
3+
// Problem Statement: Kth Largest Number in a Stream
4+
// LeetCode Question: 703. Kth Largest Element in a Stream
5+
6+
import java.util.PriorityQueue;
7+
8+
public class Problem_7_Kth_Largest_Number_In_A_Stream {
9+
PriorityQueue<Integer> minHeap = new PriorityQueue<Integer>((n1, n2) -> n1 - n2);
10+
final int k;
11+
12+
public Problem_7_Kth_Largest_Number_In_A_Stream(int[] nums, int k) {
13+
this.k = k;
14+
for (int i = 0; i < nums.length; i++)
15+
add(nums[i]);
16+
}
17+
18+
public int add(int num) {
19+
minHeap.add(num);
20+
21+
if (minHeap.size() > this.k)
22+
minHeap.poll();
23+
24+
return minHeap.peek();
25+
}
26+
}

0 commit comments

Comments
 (0)