zoukankan      html  css  js  c++  java
  • LeetCode 295. Find Median from Data Stream

    原题链接在这里:https://leetcode.com/problems/find-median-from-data-stream/

    题目:

    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

    题解:

    维护两个堆,一个maxHeap用来存较小的一半数,一个minHeap用来存较大的一半数。每次添加时看minHeap的root是不是小于 num, 若是就把num加在minHeap上,若不然就看看maxHeap的root是不是大于num, 若是就把num加在maxHeap上面.

    不是上面两种情况默认加载minHeap里.

    加完后需要balance 两个heap的大小,默认maxHeap的size不比minHeap的size大, 因为默认添加到minHeap中. 但minHeap也不能超过maxHeap的size大过1.

    findMedian  若是size相同,一边取一个算平均数, 否则从minHeap中poll出结果.

    Time Complexity: AddNum(), O(logn), n = minHeap.size(). findMedian(), O(1).

    Space: O(n). 两个heap大小.

    AC Java:

     1 class MedianFinder {
     2     
     3     PriorityQueue<Integer> minHeap;
     4     PriorityQueue<Integer> maxHeap;
     5     /** initialize your data structure here. */
     6     public MedianFinder() {
     7         minHeap = new PriorityQueue<Integer>();
     8         maxHeap = new PriorityQueue<Integer>(Collections.reverseOrder());
     9     }
    10     
    11     public void addNum(int num) {
    12         if(!maxHeap.isEmpty() && maxHeap.peek()>num){
    13             maxHeap.add(num);
    14         }else{
    15             minHeap.add(num);
    16         }
    17         
    18         if(maxHeap.size() > minHeap.size()){
    19             minHeap.add(maxHeap.poll());
    20         }else if(minHeap.size() - maxHeap.size() > 1){
    21             maxHeap.add(minHeap.poll());
    22         }
    23     }
    24     
    25     public double findMedian() {
    26         if(minHeap.size() == maxHeap.size()){
    27             return ((double)minHeap.peek() + (double)maxHeap.peek()) / 2.0;
    28         }else{
    29             return (double)minHeap.peek();
    30         }
    31     }
    32 }
    33 
    34 /**
    35  * Your MedianFinder object will be instantiated and called as such:
    36  * MedianFinder obj = new MedianFinder();
    37  * obj.addNum(num);
    38  * double param_2 = obj.findMedian();
    39  */

    类似Sliding Window Median.

  • 相关阅读:
    MongoDB配置多个ConfigDB的问题(笔记)
    Python访问PostGIS(建表、空间索引、分区表)
    Python访问MySQL数据库
    Python访问MongoDB数据库
    Mapnik读取PostGIS数据渲染图片
    Python批量处理CSV文件
    Spring Mongo配置多个Mongos
    hadoop2.2.0_hbase0.96_zookeeper3.4.5全分布式安装文档下载
    【Git】(1)---工作区、暂存区、版本库、远程仓库
    微信扫码支付功能(2)---用户扫码支付成功,微信异步回调商户接口
  • 原文地址:https://www.cnblogs.com/Dylan-Java-NYC/p/4934939.html
Copyright © 2011-2022 走看看