You're building a data processing system that needs to track the kth largest element in a continuous stream of numbers.
Design a class to find the kth largest element in a stream. Note that it is the kth largest element in the sorted order, not the kth distinct element.
Your class will have a constructor which accepts an integer k
and an integer array nums
, which contains initial elements from the stream. For each call to the method add
, return the element representing the kth largest element in the stream.
Input: KthLargest kthLargest = new KthLargest(3, [4, 5, 8, 2]);
kthLargest.add(3); // returns 4
kthLargest.add(5); // returns 5
kthLargest.add(10); // returns 5
kthLargest.add(9); // returns 8
kthLargest.add(4); // returns 8
Output: [null, 4, 5, 5, 8, 8]
Explanation: KthLargest(3, [4, 5, 8, 2]): The 3rd largest element is 4.
add(3): The elements are [2, 3, 4, 5, 8], the 3rd largest is 4.
add(5): The elements are [2, 3, 4, 5, 5, 8], the 3rd largest is 5.
add(10): The elements are [2, 3, 4, 5, 5, 8, 10], the 3rd largest is 5.
add(9): The elements are [2, 3, 4, 5, 5, 8, 9, 10], the 3rd largest is 8.
add(4): The elements are [2, 3, 4, 4, 5, 5, 8, 9, 10], the 3rd largest is 8.
To solve this problem, we need to:
Apply string manipulation concepts to solve a real-world problem.
You're building a data processing system that needs to track the kth largest element in a continuous stream of numbers.
Design a class to find the kth largest element in a stream. Note that it is the kth largest element in the sorted order, not the kth distinct element.
Your class will have a constructor which accepts an integer k
and an integer array nums
, which contains initial elements from the stream. For each call to the method add
, return the element representing the kth largest element in the stream.
KthLargest(3, [4, 5, 8, 2]): The 3rd largest element is 4. add(3): The elements are [2, 3, 4, 5, 8], the 3rd largest is 4. add(5): The elements are [2, 3, 4, 5, 5, 8], the 3rd largest is 5. add(10): The elements are [2, 3, 4, 5, 5, 8, 10], the 3rd largest is 5. add(9): The elements are [2, 3, 4, 5, 5, 8, 9, 10], the 3rd largest is 8. add(4): The elements are [2, 3, 4, 4, 5, 5, 8, 9, 10], the 3rd largest is 8.
We need to efficiently track the kth largest element in a stream of numbers
A min heap of size k can be used to maintain the k largest elements
When a new element is added, we add it to the heap and remove the smallest element if the heap size exceeds k
The top of the heap will always be the kth largest element
This approach is more efficient than sorting the entire array each time a new element is added
The problem is a classic application of the heap data structure
This problem has several practical applications:
Processing and analyzing continuous streams of data in real-time applications.
Implementing top-k queries in database management systems.
Tracking top performers or metrics in analytics dashboards.
Building systems that need to filter and maintain top k elements from a large dataset.
You're building a data processing system that needs to track the kth largest element in a continuous stream of numbers.
Design a class to find the kth largest element in a stream. Note that it is the kth largest element in the sorted order, not the kth distinct element.
Your class will have a constructor which accepts an integer k
and an integer array nums
, which contains initial elements from the stream. For each call to the method add
, return the element representing the kth largest element in the stream.
Input: KthLargest kthLargest = new KthLargest(3, [4, 5, 8, 2]);
kthLargest.add(3); // returns 4
kthLargest.add(5); // returns 5
kthLargest.add(10); // returns 5
kthLargest.add(9); // returns 8
kthLargest.add(4); // returns 8
Output: [null, 4, 5, 5, 8, 8]
Explanation: KthLargest(3, [4, 5, 8, 2]): The 3rd largest element is 4.
add(3): The elements are [2, 3, 4, 5, 8], the 3rd largest is 4.
add(5): The elements are [2, 3, 4, 5, 5, 8], the 3rd largest is 5.
add(10): The elements are [2, 3, 4, 5, 5, 8, 10], the 3rd largest is 5.
add(9): The elements are [2, 3, 4, 5, 5, 8, 9, 10], the 3rd largest is 8.
add(4): The elements are [2, 3, 4, 4, 5, 5, 8, 9, 10], the 3rd largest is 8.
To solve this problem, we need to:
Apply string manipulation concepts to solve a real-world problem.
You're building a data processing system that needs to track the kth largest element in a continuous stream of numbers.
Design a class to find the kth largest element in a stream. Note that it is the kth largest element in the sorted order, not the kth distinct element.
Your class will have a constructor which accepts an integer k
and an integer array nums
, which contains initial elements from the stream. For each call to the method add
, return the element representing the kth largest element in the stream.
KthLargest(3, [4, 5, 8, 2]): The 3rd largest element is 4. add(3): The elements are [2, 3, 4, 5, 8], the 3rd largest is 4. add(5): The elements are [2, 3, 4, 5, 5, 8], the 3rd largest is 5. add(10): The elements are [2, 3, 4, 5, 5, 8, 10], the 3rd largest is 5. add(9): The elements are [2, 3, 4, 5, 5, 8, 9, 10], the 3rd largest is 8. add(4): The elements are [2, 3, 4, 4, 5, 5, 8, 9, 10], the 3rd largest is 8.
We need to efficiently track the kth largest element in a stream of numbers
A min heap of size k can be used to maintain the k largest elements
When a new element is added, we add it to the heap and remove the smallest element if the heap size exceeds k
The top of the heap will always be the kth largest element
This approach is more efficient than sorting the entire array each time a new element is added
The problem is a classic application of the heap data structure
This problem has several practical applications:
Processing and analyzing continuous streams of data in real-time applications.
Implementing top-k queries in database management systems.
Tracking top performers or metrics in analytics dashboards.
Building systems that need to filter and maintain top k elements from a large dataset.