Kth Largest Element in a Stream - Complete Solution Guide
Kth Largest Element in a Stream is LeetCode problem 703, a Easy level challenge. This complete guide provides step-by-step explanations, multiple solution approaches, and optimized code in python3, java, cpp, c.
Problem Statement
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 k th highest test score after a new score has been submitted. More specifically, we are looking for the k th highest scor
Detailed Explanation
The problem requires you to design a class, `KthLargest`, that finds the k-th largest element in a stream of numbers in real-time. The class is initialized with an integer `k` and an initial array of numbers `nums`. The `add(val)` method adds a new number `val` to the stream and returns the k-th largest element in the stream after the addition. Note that we are looking for the k-th *largest* element in the sorted order, not the k-th *distinct* element. The stream is constantly being updated, so the k-th largest element needs to be re-evaluated with each `add` call.
Solution Approach
The solution uses a min-heap (PriorityQueue in Java, heapq in Python, priority_queue with greater comparator in C++) to store the k largest elements encountered so far. The `KthLargest` constructor initializes the heap with the initial `nums` array. The `add` method adds a new element `val` to the heap. If the size of the heap exceeds `k`, the smallest element in the heap (the root) is removed using `heappop`/`poll`/`pop`. The `add` method then returns the current root of the heap, which represents the k-th largest element.
Step-by-Step Algorithm
- Step 1: **Initialization (Constructor):** Create a min-heap and store the value of `k`. Iterate through the initial array `nums` and add each element to the heap using the `add` function.
- Step 2: **Add Element (add(val)):** Add the new element `val` to the min-heap.
- Step 3: **Maintain Heap Size:** If the size of the heap is greater than `k`, remove the smallest element from the heap (root of the min-heap). This ensures that the heap always contains only the `k` largest elements seen so far.
- Step 4: **Return k-th Largest:** Return the value at the root of the min-heap. This value is the smallest among the `k` largest elements, and therefore, it is the `k`-th largest element overall.
Key Insights
- Insight 1: Using a min-heap (priority queue) is the optimal way to keep track of the k largest elements seen so far. The min-heap will always store the k largest elements, with the smallest of these k elements at the root.
- Insight 2: When a new element is added, it's compared to the root of the min-heap. If the new element is larger than the root, the root is removed, and the new element is inserted into the heap. This ensures the heap always contains the k largest elements.
- Insight 3: The root of the min-heap will always be the k-th largest element because the heap contains the k largest elements, and since it's a min-heap, the smallest of those k is at the root.
Complexity Analysis
Time Complexity: O(nlogk)
Space Complexity: O(k)
Topics
This problem involves: Tree, Design, Binary Search Tree, Heap (Priority Queue), Binary Tree, Data Stream.
Companies
Asked at: Arista Networks, Atlassian, Box, Tinder, Wells Fargo.