The problem. Design a class MedianFinder for a stream of integers. addNum(num) adds a number. findMedian() returns the median of all numbers added so far: the middle value in sorted order, or the average of the two middle values when the count is even.
addNum(1)
addNum(2)
findMedian() -> 1.5 (1 and 2: the average of the two middle values)
addNum(3)
findMedian() -> 2 (1, 2, 3: the middle value)Members only · free account
The rest of this lesson, with 7 more sections, 1 drawing, a dry-run simulator and code in JavaScript, Python, Java and C++, opens with a free account.
Was this helpful?
Not ready yet? The Arrays course is free to read, no account needed.
In the rest of this lesson