一 题目
如何得到一个数据流中的中位数?如果从数据流中读出奇数个数值,那么中位数就是所有数值排序之后位于中间的数值。如果从数据流中读出偶数个数值,那么中位数就是所有数值排序之后中间两个数的平均值。
二 例子
①输入 5 2
输出 5.0 3.5
②输入 5 2 3
输出 5.0 3.5 3.0
三 思路
Java的PriorityQueue 是从JDK1.5开始提供的新的数据结构接口,默认内部是自然排序,结果为小顶堆,也可以自定义排序器,比如下面反转比较,完成大顶堆。只需要在new的时候传入一个comparator即可。
为了保证插入新数据和取中位数的时间效率都高效,这里使用大顶堆+小顶堆的容器,并且满足:
1、两个堆中的数据数目差不能超过1,这样可以使中位数只会出现在两个堆的交接处;
2、大顶堆的所有数据都小于小顶堆,小顶堆的所有数据都大于大顶堆,这样就满足了排序要求。
四 程序源码
- public class Solution {
- private PriorityQueue<Integer> minQueue = new PriorityQueue<>();
- private PriorityQueue<Integer> maxQueue = new PriorityQueue<>(15,new Comparator<Integer>() {
- @Override
- public int compare(Integer o1, Integer o2) {
- return o2 - o1;
- }
- });
- int count = 0;
- public void Insert(Integer num) {
- count++;
- if ((count&1)==0){
- if (!maxQueue.isEmpty()&&num<maxQueue.peek()){
- maxQueue.offer(num);
- num = maxQueue.poll();
- }
- minQueue.offer(num);
- }else{
- if (!minQueue.isEmpty()&&num>minQueue.peek()){
- minQueue.offer(num);
- num = minQueue.poll();
- }
- maxQueue.offer(num);
- }
- }
- public Double GetMedian() {
- if ((count&1)==0)
- return ((double)(maxQueue.peek() + minQueue.peek()))/2;
- else
- return (double)maxQueue.peek();
- }
- }