zoukankan      html  css  js  c++  java
  • Find Median from Data Stream 解答

    Question

    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:

    add(1)
    add(2)
    findMedian() -> 1.5
    add(3) 
    findMedian() -> 2

    Solution 1 -- Heap

    我们可以依据这个思路来想这道题:

    median -> 中位数 -> 中位数两边的subarrays的大小差不超过1 -> 什么数据结构可以满足实时地将输入数组排序,并且保持一半一半的大小?

    第一选择是Heap

    我们维护一个MaxHeap用来存前半段的小的数据,维护一个MinHeap用来存后半段的大的数据

    每次加入一个新数字,我们进行如下顺序判断:

    1. MaxHeap是否是空,如果是,加入MaxHeap

    2. num是否小于MaxHeap的最大值,如果是,加入MaxHeap

    3. MinHeap是否是空,如果是,加入MinHeap

    4. MaxHeap和MinHeap都不空

    num 和 MaxHeap的最大值 和 MinHeap的最小值 比较

     1 class MedianFinder {
     2     private PriorityQueue<Integer> minHeap;
     3     private PriorityQueue<Integer> maxHeap;
     4     private Double median;
     5     
     6     public MedianFinder() {
     7         minHeap = new PriorityQueue<Integer>(11);
     8         maxHeap = new PriorityQueue<Integer>(11, Collections.reverseOrder());
     9         median = null;
    10     }
    11     
    12     // Adds a number into the data structure.
    13     public void addNum(int num) {
    14         int size1 = maxHeap.size();
    15         int size2 = minHeap.size();
    16         if (size1 == 0) {
    17             maxHeap.add(num);
    18         } else if (num <= maxHeap.peek()) {
    19             maxHeap.add(num);
    20         } else if (size2 == 0) {
    21             minHeap.add(num);
    22         }else {
    23             int firstMax = maxHeap.peek();
    24             int secondMin = minHeap.peek();
    25             if (num <= firstMax) {
    26                 maxHeap.add(num);
    27             } else if (num >= secondMin) {
    28                 minHeap.add(num);
    29             } else{
    30                 maxHeap.add(num);
    31             }
    32         }
    33         // Check balance
    34         size1 = maxHeap.size();
    35         size2 = minHeap.size();
    36         if (size1 > size2 + 1) {
    37             while (size1 > size2 + 1) {
    38                 int top = maxHeap.poll();
    39                 minHeap.offer(top);
    40                 size1--;
    41                 size2++;
    42             }
    43         } else if (size2 > size1 + 1) {
    44             while (size2 > size1 + 1) {
    45                 int top = minHeap.poll();
    46                 maxHeap.offer(top);
    47                 size2--;
    48                 size1++;
    49             }
    50         }
    51         if (size2 == size1 + 1)
    52             median = (double) minHeap.peek();
    53         if (size1 == size2 + 1)
    54             median = (double) maxHeap.peek();
    55         if (size1 == size2 && size1 > 0)
    56             median = ((double) maxHeap.peek() + (double) minHeap.peek()) / 2;
    57     }
    58 
    59     // Returns the median of current data stream
    60     public double findMedian() {
    61         return median.doubleValue();
    62     }
    63 };
    64 
    65 // Your MedianFinder object will be instantiated and called as such:
    66 // MedianFinder mf = new MedianFinder();
    67 // mf.addNum(1);
    68 // mf.findMedian();

    Solution 2 -- Balanced BST

    平衡二叉树也可以满足条件,但是实际代码太多。所以可以在面试时优先用heap写出代码,然后follow-up的时候提出平衡二叉树的思想。

  • 相关阅读:
    iOS 应用内付费(IAP)开发步骤
    国内银行CNAPS CODE 查询 苹果开发者,应用内购,需要填写税务相关信息必须的
    untiy 插件工具: 游戏中 策划数据Excel 导出到项目中
    大陆 Google play 开发者注册(2016)
    unity UGUI动态字体显示模糊
    Vue--webpack实时重新加载
    Vue--webpack打包css、image资源
    Vue--webpack打包js文件
    Vue--axios
    Vue--生命周期
  • 原文地址:https://www.cnblogs.com/ireneyanglan/p/4916190.html
Copyright © 2011-2022 走看看