Design a data structure that supports adding numbers and finding their median.
The median is:
Implement the MedianFinder
class:
MedianFinder()
initializes the objectaddNum(num: number)
adds an integer from the streamfindMedian(): number
returns the current medianConstraints:
findMedian
will only be called after adding at least one numberaddNum
and findMedian
Examples:
const medianFinder = new MedianFinder(); medianFinder.addNum(1); // [1] medianFinder.findMedian(); // returns 1.0 medianFinder.addNum(3); // [1, 3] medianFinder.findMedian(); // returns 2.0 medianFinder.addNum(2); // [1, 2, 3] medianFinder.findMedian(); // returns 2.0