<div>
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 theMedianFinderobject.void addNum(int num)adds the integernumfrom the data stream to the data structure.double findMedian()returns the median of all elements so far. Answers within10<sup>-5</sup>of the actual answer will be accepted.
Example 1:
<pre>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 </pre>
Constraints:
-10<sup>5</sup> <= num <= 10<sup>5</sup>- There will be at least one element in the data structure before calling
findMedian. - At most
5 * 10<sup>4</sup>calls will be made toaddNumandfindMedian.
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?
</div>
题目大意:
求动态中位数
算法思路:
用max_heap, min_heap, 保证min_heap个数永远等于max_heap或多一个。插入元素先进max_heap, 再heappop将堆顶元素加入min_heap, 若此时比max_heap多2个,就再heappop加入到max_heap.
注意事项:
- 中位数有1-2个
- Python中的max_heap用负数实现,入堆出堆都要取反
Python代码:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19class MedianFinder(TestCases):
def __init__(self):
self.max_heap = []
self.min_heap = []
def addNum(self, num: int) -> None:
heapq.heappush(self.max_heap, -num)
max_value = -heapq.heappop(self.max_heap)
heapq.heappush(self.min_heap, max_value)
if len(self.min_heap) - len(self.max_heap) >= 2:
min_value = heapq.heappop(self.min_heap)
heapq.heappush(self.max_heap, -min_value)
def findMedian(self) -> float:
if len(self.max_heap) == len(self.min_heap):
return (-self.max_heap[0] + self.min_heap[0]) / 2
else:
return self.min_heap[0]
算法分析:
时间复杂度为O(logn),空间复杂度O(n)


