Leetcode 703: Kth Largest Element in a Stream
You are tasked with implementing a class that helps track the kth highest score in a dynamic list of test scores. Each time a new score is submitted, you need to return the kth highest score in the list.
Problem
Approach
Steps
Complexity
Input: The class should be initialized with an integer k (the desired ranking) and a list of test scores. For each new score added to the stream, return the kth highest score in the list.
Example: KthLargest kthLargest = new KthLargest(2, [1, 3, 5, 7, 9]); kthLargest.add(6);
Constraints:
• 0 <= nums.length <= 10^4
• 1 <= k <= nums.length + 1
• -10^4 <= nums[i] <= 10^4
• -10^4 <= val <= 10^4
• At most 10^4 calls will be made to the add method.
Output: The method should return the kth largest number in the list after each new score is added.
Example: Output: [null, 5, 6, 7, 7]
Constraints:
• The result after each call to add should be the kth largest score in the current list of test scores.
Goal: Track the kth largest score dynamically using a priority queue or a min-heap to efficiently maintain the list of the top k scores.
Steps:
• 1. Initialize a priority queue (min-heap) to store the top k elements.
• 2. Add each score to the heap.
• 3. If the heap exceeds k elements, remove the smallest element.
• 4. After each addition, the top of the heap will be the kth largest score.
Goal: The problem must handle dynamic updates to the list of scores.
Steps:
• The class must handle multiple calls to the add method efficiently.
• The list of scores can vary in size from 0 to 10,000.
Assumptions:
• The list will be dynamically updated with new scores.
• The kth largest element should be returned after each new score is added.
• Input: Input: KthLargest(2, [1, 3, 5, 7, 9])
• Explanation: When the 2nd largest element is requested after each new score is added, the following occurs: add(6) => 6 becomes the 2nd largest number, add(8) => 7 becomes the 2nd largest number, etc.
Approach: The goal is to efficiently manage a dynamic list of scores and find the kth largest element after each insertion. A priority queue or min-heap can be used to keep track of the top k elements in the stream.
Observations:
• A heap is a good choice to manage a dynamic list of top k elements efficiently.
• Since we are only interested in the kth largest element, using a min-heap of size k ensures that the smallest element in the heap is always the kth largest.
Steps:
• Step 1: Initialize the KthLargest class with an integer k and a list of scores.
• Step 2: Use a priority queue (min-heap) to store the top k elements.
• Step 3: Add a new score to the heap and ensure the heap contains at most k elements.
• Step 4: After each insertion, return the smallest element in the heap, which represents the kth largest score.
Empty Inputs:
• The list of scores may be empty initially, but k will always be valid.
Large Inputs:
• If the number of scores is very large, ensure that the heap operations are optimized to handle up to 10,000 elements.
Special Values:
• If multiple scores have the same value, they should be treated as equal for the purposes of finding the kth largest score.
Constraints:
• Ensure the heap is only storing the top k elements at all times to maintain efficiency.
priority_queue<int, vector<int>, greater<int>> pq;
int k;
KthLargest(int k, vector<int>& nums) {
this->k = k;
for(auto x: nums) {
pq.push(x);
if(pq.size() > k) {
cout << pq.top();
pq.pop();
}
}
}
int add(int val) {
pq.push(val);
if(pq.size() > k) {
pq.pop();
}
return pq.top();
}
};
/**
* Your KthLargest object will be instantiated and called as such:
* KthLargest* obj = new KthLargest(k, nums);
* int param_1 = obj->add(val);
1 : Priority Queue Initialization
priority_queue<int, vector<int>, greater<int>> pq;
A min-heap priority queue 'pq' is initialized to keep track of the K largest elements. The 'greater<int>' ensures the smallest element is at the top.
2 : Variable Declaration
int k;
The variable 'k' holds the value of K, which represents the number of largest elements to track.
3 : Constructor Definition
KthLargest(int k, vector<int>& nums) {
Constructor for the KthLargest class. It initializes 'k' and populates the priority queue 'pq' with the values from the input vector 'nums'.
4 : Variable Initialization
this->k = k;
Assign the input value 'k' to the class variable 'k'.
5 : Loop Over Input Array
for(auto x: nums) {
Iterate over each element in the input array 'nums' to populate the priority queue.
6 : Push to Priority Queue
pq.push(x);
Push the current element 'x' from the input array into the priority queue.
7 : Check Priority Queue Size
if(pq.size() > k) {
Check if the priority queue contains more than 'k' elements.
8 : Remove Smallest Element
cout << pq.top();
Output the smallest element in the priority queue, which is at the top.
9 : Pop Top Element
pq.pop();
Remove the smallest element from the priority queue to maintain only the K largest elements.
10 : Method Definition
int add(int val) {
Method that adds a new value 'val' to the priority queue and returns the current Kth largest element.
11 : Push Value to Queue
pq.push(val);
Push the new value 'val' into the priority queue.
12 : Check Queue Size
if(pq.size() > k) {
Check if the size of the priority queue exceeds 'k'.
13 : Pop Smallest Element
pq.pop();
Pop the smallest element from the priority queue to ensure it only contains the largest 'k' elements.
14 : Return Kth Largest Element
return pq.top();
Return the top element of the priority queue, which is the Kth largest element.
Best Case: O(log k) for each add operation when the heap is not full.
Average Case: O(log k) for each add operation.
Worst Case: O(log k) for each add operation, where k is the number of elements in the heap.
Description: The time complexity of adding a new score is logarithmic relative to k, the size of the heap.
Best Case: O(k), since the space required does not depend on the number of operations but only on k.
Worst Case: O(k), as we only need to store the top k elements in the heap.
Description: The space complexity is determined by the size of the heap, which stores the top k elements.
LeetCode Solutions Library / DSA Sheets / Course Catalog |
---|
comments powered by Disqus