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 is 3
.
For example, for arr = [2,3]
, the median is (2 + 3) / 2 = 2.5
.Implement the MedianFinder class:
MedianFinder()
initializes the MedianFinder
object.
void addNum(int num)
adds the integer num
from the data stream to the data structure.double findMedian()
returns the median of all elements so far. Answers within 10<sup>-5</sup>
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<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 to addNum
and findMedian
.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?题目大意:
求动态中位数
算法思路:
用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 | class MedianFinder(TestCases): |
算法分析:
时间复杂度为O(logn)
,空间复杂度O(n)