题目:
Median is the middle value in an ordered integer list. If the size of the list is even, there is no middle value. So the median is the mean of the two middle value.
Examples: [2,3,4]
, the median is 3
[2,3]
, the median is (2 + 3) / 2 = 2.5
Design a data structure that supports the following two operations:
- void addNum(int num) - Add a integer number from the data stream to the data structure.
- double findMedian() - Return the median of all elements so far.
For example:
addNum(1) addNum(2) findMedian() -> 1.5 addNum(3) findMedian() -> 2
思路:
最优解是维护一个大顶堆和一个小顶堆,并且保证大顶堆的大小不小于小顶堆的大小,其中大顶堆里面包含数组中小的一半元素,小顶堆里面包含数组中大的一半元素。这样大顶堆的首元素是小一半里面最大的元素,而小顶堆的首元素是大一半里面最小的元素。如果大顶堆和小顶堆的大小相同,则中位数就是两者首元素的平均值;否则就是大顶堆的首元素。而添加一个元素的方法是:首先判断这个元素应该出现在大顶堆还是小顶堆,并且将其插入相应的堆中。之后还要根据情况调整两个堆的大小,保证大顶堆比小顶堆多一个元素或者两者元素数相同。一个巧妙的方法是:首先将该元素插入大顶堆中,然后将大顶堆的首元素移到小顶堆中;移动之后如果发现大顶堆的大小小于小顶堆,则将小顶堆的首元素移到大顶堆中。
在C++的实现中,既可以用数组来模拟heap实现,也可以用priority_queue来实现,还可以用multiset来实现,非常方便。
代码:
class MedianFinder {
public:
/** initialize your data structure here. */
MedianFinder() {
}
void addNum(int num) { // the time complexity is O(logn)
small.push(num);
large.push(-small.top());
small.pop();
if(small.size() < large.size()) {
small.push(-large.top());
large.pop();
}
}
double findMedian() { // the time complexity is O(1)
int size = small.size() + large.size();
if (size % 2 != 0) {
return small.top();
}
else {
return (small.top() - large.top()) / 2.0;
}
}
private:
priority_queue<int> small, large;
};
/**
* Your MedianFinder object will be instantiated and called as such:
* MedianFinder obj = new MedianFinder();
* obj.addNum(num);
* double param_2 = obj.findMedian();
*/