The median is the middle value in an ordered integer list. If the size of the list is even, there is no middle value, and the median is the mean of the two middle values.
- For example, for
arr = [2,3,4]
, the median is3
. - For example, for
arr = [2,3]
, the median is(2 + 3) / 2 = 2.5
.
Implement the MedianFinder class:
MedianFinder()
initializes theMedianFinder
object.void addNum(int num)
adds the integernum
from the data stream to the data structure.double findMedian()
returns the median of all elements so far. Answers within105
of the actual answer will be accepted.
Example 1:
Input
["MedianFinder", "addNum", "addNum", "findMedian", "addNum", "findMedian"]
[[], [1], [2], [], [3], []]
Output
[null, null, null, 1.5, null, 2.0]
Explanation
MedianFinder medianFinder = new MedianFinder();
medianFinder.addNum(1); // arr = [1]
medianFinder.addNum(2); // arr = [1, 2]
medianFinder.findMedian(); // return 1.5 (i.e., (1 + 2) / 2)
medianFinder.addNum(3); // arr[1, 2, 3]
medianFinder.findMedian(); // return 2.0
Constraints:
-10^5 <= num <= 10^5
- There will be at least one element in the data structure before calling
findMedian
. - At most
5 * 10^4
calls will be made toaddNum
andfindMedian
.
Follow up:
- If all integer numbers from the stream are in the range
[0, 100]
, how would you optimize your solution? - If
99%
of all integer numbers from the stream are in the range[0, 100]
, how would you optimize your solution?
- Maintain Max Heap (contain smaller part of elements) and Min Heap (represent large part of elements)
- If number of elements is odd, get first element from Max Heap is median. If it is even, get first both from Max and Min Heap then added together, than divided by two is median
- Tip:
- use
value * -1
to use Min Heap represent Max Heap
- use
-
GoLang
Runtime 350 ms Beats 84%
Memory 19.9 MB Beats 59.11%
Time Complexity:
AddNum()
takesO(NlogN)
FindMedian()
takes O(1)
Space Complexity:
O(N)
type IntHeap []int func (h IntHeap) Len() int { return len(h) } func (h IntHeap) Less(i, j int) bool { return h[i] < h[j] } func (h IntHeap) Swap(i, j int) { h[i], h[j] = h[j], h[i] } func (h *IntHeap) Push(x any) { // Push and Pop use pointer receivers because they modify the slice's length, // not just its contents. *h = append(*h, x.(int)) } func (h *IntHeap) Pop() any { old := *h n := len(old) x := old[n-1] *h = old[0 : n-1] return x } type MedianFinder struct { small IntHeap large IntHeap } func Constructor() MedianFinder { return MedianFinder{ small: IntHeap{}, large: IntHeap{}, } } func (this *MedianFinder) AddNum(num int) { if len(this.large) > 0 && num > this.large[0] { heap.Push(&this.large, num) } else { heap.Push(&this.small, -1 * num) } // move element from samll to large if small more than large 2 if len(this.small) > len(this.large) + 1 { val := -1 * heap.Pop(&this.small).(int) heap.Push(&this.large, val) } if len(this.large) > len(this.small) + 1 { val := -1 * heap.Pop(&this.large).(int) heap.Push(&this.small, val) } } func (this *MedianFinder) FindMedian() float64 { // when elments amount is odd if len(this.small) > len(this.large) { return float64(-1 * this.small[0]) } else if len(this.large) > len(this.small) { return float64(this.large[0]) } // when elements amount is even return float64(this.large[0] + -1 * this.small[0]) / 2 } /** * Your MedianFinder object will be instantiated and called as such: * obj := Constructor(); * obj.AddNum(num); * param_2 := obj.FindMedian(); */