The Question
CodingMedian from Data Stream
Design a data structure that supports receiving a stream of integers and efficiently retrieving the median of all elements seen so far.
The
MedianFinder class should implement:
1. void addNum(int num): Adds an integer to the data set.
2. double findMedian(): Returns the median of all current elements. If the number of elements is even, the median is the mean of the two middle values.
Constraints:
- At most 10^5 calls will be made to addNum and findMedian.
- Values of integers are within the range [-10^5, 10^5].
- Optimize for both time complexity of insertion and retrieval.C++
Heaps
Priority Queue