码迷,mamicode.com
首页 > 其他好文 > 详细

295. Find Median from Data Stream

时间:2015-12-14 14:03:35      阅读:251      评论:0      收藏:0      [点我收藏+]

标签:

题目:

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

链接: http://leetcode.com/problems/find-median-from-data-stream/

题解:

在Data stream中找到median。这道题是Heap的经典应用,需要同时维护一个最大堆和一个最小堆, 最大堆和最小堆的size <= 当前数字count / 2。在学习heap数据结构的时候一般都会讲到这一题,很经典。

Time Complexity: addNum - O(logn)  , findMedian - O(1),  Space Complexity - O(n)

class MedianFinder {
    private PriorityQueue<Integer> maxOrientedHeap;
    private PriorityQueue<Integer> minOrientedHeap;
    
    public MedianFinder() {
        this.minOrientedHeap = new PriorityQueue<Integer>();
        this.maxOrientedHeap = new PriorityQueue<Integer>(10, new Comparator<Integer>() {
                public int compare(Integer i1, Integer i2) {
                    return i2 - i1;
                }
            });
    }
    // Adds a number into the data structure.
    public void addNum(int num) {
        maxOrientedHeap.add(num);               // O(logn)
        minOrientedHeap.add(maxOrientedHeap.poll());               // O(logn)
        if(maxOrientedHeap.size() < minOrientedHeap.size()) {
            maxOrientedHeap.add(minOrientedHeap.poll());        //O(logn)
        }
    }

    // Returns the median of current data stream
    public double findMedian() {                    // O(1)
        if(maxOrientedHeap.size() == minOrientedHeap.size())
            return (maxOrientedHeap.peek() + minOrientedHeap.peek()) / 2.0;
        else
            return maxOrientedHeap.peek();
    }
};

// Your MedianFinder object will be instantiated and called as such:
// MedianFinder mf = new MedianFinder();
// mf.addNum(1);
// mf.findMedian();

 

Reference:

https://leetcode.com/discuss/65107/share-my-java-solution-logn-to-insert-o-1-to-query

https://leetcode.com/discuss/64850/short-simple-java-c-python-o-log-n-o-1

https://leetcode.com/discuss/64811/easy-to-understand-double-heap-solution-in-java

https://leetcode.com/discuss/64910/very-short-o-log-n-o-1

https://leetcode.com/discuss/64842/32ms-easy-to-understand-java-solution

https://leetcode.com/discuss/68290/simple-java-solution-with-2-heaps-and-explanation

https://leetcode.com/discuss/64852/java-python-two-heap-solution-o-log-n-add-o-1-find

295. Find Median from Data Stream

标签:

原文地址:http://www.cnblogs.com/yrbbest/p/5044819.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!