703. Kth Largest Element in a Stream
DifficultyEasy
Description
You are part of a university admissions office and need to keep track of the kth highest test score from applicants in real-time. This helps to determine cut-off marks for interviews and admissions dynamically as new applicants submit their scores.
You are tasked to implement a class which, for a given integer k, maintains a stream of test scores and continuously returns the kth highest test score after a new score has been submitted. More specifically, we are looking for the kth highest score in the sorted list of all scores.
Implement the KthLargest class:
KthLargest(int k, int[] nums)Initializes the object with the integerkand the stream of test scoresnums.int add(int val)Adds a new test scorevalto the stream and returns the element representing thekthlargest element in the pool of test scores so far.
Example 1:
Input:
["KthLargest", "add", "add", "add", "add", "add"]
[[3, [4, 5, 8, 2]], [3], [5], [10], [9], [4]]
Output: [null, 4, 5, 5, 8, 8]
Explanation:
KthLargest kthLargest = new KthLargest(3, [4, 5, 8, 2]);
kthLargest.add(3); // return 4
kthLargest.add(5); // return 5
kthLargest.add(10); // return 5
kthLargest.add(9); // return 8
kthLargest.add(4); // return 8
Example 2:
Input:
["KthLargest", "add", "add", "add", "add"]
[[4, [7, 7, 7, 7, 8, 3]], [2], [10], [9], [9]]
Output: [null, 7, 7, 7, 8]
Explanation:
KthLargest kthLargest = new KthLargest(4, [7, 7, 7, 7, 8, 3]);kthLargest.add(2); // return 7
kthLargest.add(10); // return 7
kthLargest.add(9); // return 7
kthLargest.add(9); // return 8
Constraints:
0 <= nums.length <= 1041 <= k <= nums.length + 1-104 <= nums[i] <= 104-104 <= val <= 104- At most
104calls will be made toadd.
Solutions
Solution 1: Priority Queue (Min Heap)
Thinking
We insert into a stream and must report the \(k\)-th largest after each add. Sorting or scanning the whole history on every query is too heavy when both \(n\) and the query count reach \(10^4\).
Only the current top \(k\) values matter; anything smaller can never be the answer. In a size-\(k\) min-heap the top is the smallest of those \(k\), i.e. the \(k\)-th largest overall.
Push each new value and pop if the heap grows past \(k\). Seeding from \(\textit{nums}\) uses the same \(\textit{add}\). Each update is \(O(\log k)\).
We maintain a priority queue (min heap) \(\textit{minQ}\).
Initially, we add the elements of the array \(\textit{nums}\) to \(\textit{minQ}\) one by one, ensuring that the size of \(\textit{minQ}\) does not exceed \(k\). The time complexity is \(O(n \times \log k)\).
Each time a new element is added, if the size of \(\textit{minQ}\) exceeds \(k\), we pop the top element of the heap to ensure that the size of \(\textit{minQ}\) is \(k\). The time complexity is \(O(\log k)\).
In this way, the elements in \(\textit{minQ}\) are the largest \(k\) elements in the array \(\textit{nums}\), and the top element of the heap is the \(k^{th}\) largest element.
The space complexity is \(O(k)\).
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | |
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 | |
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 | |
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 | |
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 | |
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 | |