7. Find Median from Data Stream
Median is the middle value in an ordered integer list. If the size of the list is even, there is no middle value. So the median is the mean of the two middle value.For example,
[2,3,4]
, the median is 3
[2,3]
, the median is (2 + 3) / 2 = 2.5
Design a data structure that supports the following two operations:
void addNum(int num) - Add a integer number from the data stream to the data structure.
double findMedian() - Return the median of all elements so far.
Example:
Solution: (Bruteforce)
Inserting into a vector. Sorting the array and finding the median Time Complexity: (n log n) for 1 query
Solution: (Insertion Sort)
Median can be accessed in O(1) time Inserting the num always in the sorted position (Finding the position and inserting) O(n)
Time Complexity: O(n)
Solution: (Using Heap)
We can maintain two heap :
Two priority queues:
A max-heap
lo
to store the smaller half of the numbersA min-heap
hi
to store the larger half of the numbers
Insertion will take O(log n) .
The max-heap lo
is allowed to store, at worst, one more element more than the min-heap hi
. When the heaps are perfectly balanced, the median can be derived from the tops of both heaps. Median can be assessed in O(1) time.
Adding a number num
:
Add
num
to max-heaplo
. Sincelo
received a new element, we must do a balancing step forhi
. So remove the largest element fromlo
and offer it tohi
.The min-heap
hi
might end holding more elements than the max-heaplo
, after the previous operation. We fix that by removing the smallest element fromhi
and offering it tolo
.
Approach: [41, 35, 62, 5, 97, 108]
Time Complexity: O(log n)
Last updated