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)Free account
Sign up to read the rest of this lesson: 7 more sections, 2 drawings, a dry-run simulator and code in JavaScript, Python, Java and C++.
Still to come
Was this helpful?