题目:如何得到一个数据流中的中位数?如果从数据流中读出奇数个数值,那么中位数就是所有数值排序之后位于中间的数值。如果从数据流中读出偶数个数值,那么中位数就是所有数值排序之后中间两个数的平均值。
思路:
1.考虑用最大堆和最小堆实现。为了实现平均分配,可以在数据的总数目是偶数是把新数据插入最小堆,否则插入最大堆。
2.还要保证最大堆中的所有数据都要小于最小堆中的数据。可以先按照奇偶规则插入,再适当调整。
看了左神的解法,结合书上的解法,自己写了一下,没有测试过,之后测试了看看,JAVA参考代码如下:
public class Solution {
public class BiggerComparator implements Comparator<Integer>{
public int compare(Integer o1,Integer o2){
return o2-o1;
}
}
public class SmallerComparator implements Comparator<Integer>{
public int compare(Integer o1,Integer o2){
return o1-o2;
}
}
private PriorityQueue<Integer> maxQue=new PriorityQueue<Integer>(new BiggerComparator());
private PriorityQueue<Integer> minQue=new PriorityQueue<Integer>(new SmallerComparator());
private int totalSize=0;
public void Insert(Integer num) {
totalSize=maxQue.size()+minQue.size();
if((totalSize&1)==0){
if(num<maxQue.peek()){
maxQue.add(num);
minQue.add(maxQue.poll());
}
minQue.add(num);
}else{
if(num>minQue.peek()){
minQue.add(num);
maxQue.add(minQue.poll());
}
maxQue.add(num);
}
}
public Double GetMedian() {
totalSize=maxQue.size()+minQue.size();
if(totalSize==0){
throw new Exception();
}
if((totalSize&1)==1){
return (Double)minQue.peek();
}else{
return (Double)(maxQue.peek()+minQue.peek())/2;
}
}
}
基于以上思路,C++参考代码如下:
class Solution {
private:
vector<int> min; //数组中的后一半元素组成一个最小化堆
vector<int> max; //数组中的前一半元素组成一个最大化堆
public:
void Insert(int num) {
if(((min.size()+max.size()) & 1) == 0) { //偶数数据的情况下,则在最小堆中插入元素
if(max.size() > 0 && num < max[0]) { //插入的数据有比最大堆中的数据小(当然就比最大堆的最大值小)
max.push_back(num);//先把这个新的数据插入最大堆
push_heap(max.begin(), max.end(), less<int>());
num=max[0];//接着把最大堆中最大的数字拿出来插入到最小堆
pop_heap(max.begin(), max.end(), less<int>());
max.pop_back();
}
min.push_back(num);
push_heap(min.begin(), min.end(), greater<int>());
} else { //奇数数据的情况下,则在最大堆中插入元素
if(min.size() > 0 && num > min[0]) { //插入的数据有比最小堆中的数据大(当然就比最小堆的最小值大)
min.push_back(num);先把这个新的数据插入最小堆
push_heap(min.begin(), min.end(), greater<int>());
num=min[0];接着把最小堆中最小的数字拿出来插入到最大堆
pop_heap(min.begin(), min.end(), greater<int>());
min.pop_back();
}
max.push_back(num);
push_heap(max.begin(), max.end(), less<int>());
}
}
double GetMedian() {
int size=min.size() + max.size();
if(size==0) return -1;
double median = 0;
if((size&1) != 0) {
median = (double) min[0];
} else {
median = (double) (max[0] + min[0]) / 2;
}
return median;
}
};
java参考代码如下:
private int count = 0;
private PriorityQueue<Integer> minHeap = new PriorityQueue<>();
private PriorityQueue<Integer> maxHeap = new PriorityQueue<Integer>(15, new Comparator<Integer>()
{
@Override public int compare(Integer o1, Integer o2)
{
return o2 - o1;
}
});
//读入字符,放到合适位置
public void Insert(Integer num)
{
if (count %2 == 0)
{
maxHeap.offer(num);
int filteredMaxNum = maxHeap.poll();
minHeap.offer(filteredMaxNum);
}
else
{
minHeap.offer(num);
int filteredMinNum = minHeap.poll();
maxHeap.offer(filteredMinNum);
}
count++;
}
//求中位数
public Double GetMedian()
{
if (count %2 == 0)
{
return new Double((minHeap.peek() + maxHeap.peek())) / 2;
}
else
{
return new Double(minHeap.peek());
}
}
a. 为了保证两个堆中的数据数目差不能超过1,在Insert()方法中使用了count来辅助实现。
b. 为了保证小顶堆的元素都小于大顶堆的元素,借用优先队列PriorityQueue。其默认维持队列内升序排列。也可以像上面传入一个比较器,然后使其改变排列顺序。
c. 具体的实施方案。当数据总数为偶数时,新加入的元素,应当进入小根堆,注意不是直接进入小根堆,而是经大根堆筛选后取大根堆中最大元素进入小根堆;当数据总数为奇数时,新加入的元素,应当进入大根堆。注意不是直接进入大根堆,而是经小根堆筛选后取小根堆中最大元素进入大根堆。
测试用例:
a.功能测试(从数据流中读取奇数个数字;从数据流中读取偶数个数字)。
b.边界值测试(从数据流中读出0个、1个、2个数字)。
参考:
http://www.cnblogs.com/easonliu/p/4441916.html
https://blog.csdn.net/ouyangyanlan/article/details/72875917