出自:http://www.zhenganwen.top
已获授权
如何得到一个数据流中的中位数?如果从数据流中读出奇数个数值,那么中位数就是所有数值排序之后位于中间的数值。如果从数据流中读出偶数个数值,那么中位数就是所有数值排序之后中间两个数的平均值。我们使用Insert()方法读取数据流,使用GetMedian()方法获取当前读取数据的中位数。
public void Insert(Integer num) {
}
public Double GetMedian() {
}
由于中位数只与排序后位于数组中间的一个数或两个数相关,而与数组两边的其它数无关,因此我们可以用一个大根堆保存数组左半边的数的最大值,用一个小根堆保存数组右半边的最小值,插入元素 O(logn)
,取中位数 O(1)
。
public class Solution {
//小根堆、大根堆
PriorityQueue<Integer> minHeap = new PriorityQueue(new MinRootHeadComparator());
PriorityQueue<Integer> maxHeap = new PriorityQueue(new MaxRootHeadComparator());
int count = 0;
class MaxRootHeadComparator implements Comparator<Integer>{
//返回值大于0则认为逻辑上i2大于i1(无关对象包装的数值)
public int compare(Integer i1, Integer i2){
return i2.intValue() - i1.intValue();
}
}
class MinRootHeadComparator implements Comparator<Integer>{
public int compare(Integer i1, Integer i2){
return i1.intValue() - i2.intValue();
}
}
public void Insert(Integer num) {
count++;//当前这个数是第几个进来的
//编号是奇数就放入小根堆(右半边),否则放入大根堆
if(count % 2 != 0){
//如果要放入右半边的数比左半边的最大值要小则需调整左半边的最大值放入右半边并将当前这个数放入左半边,这样才能保证右半边的数都比左半边的大
if(maxHeap.size() > 0 && num < maxHeap.peek()){
maxHeap.add(num);
num = maxHeap.poll();
}
minHeap.add(num);
}else{
if(minHeap.size() > 0 && num > minHeap.peek()){
minHeap.add(num);
num = minHeap.poll();
}
maxHeap.add(num);
}
}
public Double GetMedian() {
if(count == 0){
return 0.0;
}
if(count % 2 != 0){
return minHeap.peek().doubleValue();
}else{
return (minHeap.peek().doubleValue() + maxHeap.peek().doubleValue()) / 2;
}
}
}