zoukankan      html  css  js  c++  java
  • LeetCode 295. Find Median from Data Stream数据流的中位数 (C++/Java)

    题目:

    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.

    For example,

    [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.

    Example:

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

    分析:

    中位数是有序列表中间的数。如果列表长度是偶数,中位数则是中间两个数的平均值。

    很明显我们最好不要每调用一次求中位数函数就重新计算一遍,这样做时间复杂度较高。

     剑指offer中有一道相同的题目,可以参考这篇理解这道题,剑指Offer-63.数据流中的中位数(C++/Java)

    程序:

    C++

    class MedianFinder {
    public:
        /** initialize your data structure here. */
        MedianFinder() {
            index = 0;
        }
        
        void addNum(int num) {
            if(index % 2 == 0){
                minHeap.push(num);
                maxHeap.push(minHeap.top());
                minHeap.pop();
            }
            else{
                maxHeap.push(num);
                minHeap.push(maxHeap.top());
                maxHeap.pop();
            }
            index++;
        }
        
        double findMedian() {
            double res = 0;
            if(index % 2 == 0){
                res = (double)(maxHeap.top() + minHeap.top()) / 2;
                return res;
            }
            else{
                res = (double)maxHeap.top();
                return res;
            }
        }
    private:
        priority_queue <int, vector<int>, less<int> > maxHeap;
        priority_queue <int, vector<int>, greater<int> > minHeap;
        int index;
    };

    Java

    class MedianFinder {
    
        /** initialize your data structure here. */
        public MedianFinder() {
            minHeap = new PriorityQueue<Integer>();
            maxHeap = new PriorityQueue<Integer>(11,new Comparator<Integer>(){
                @Override
                public int compare(Integer i1,Integer i2){
                    return i2-i1;
                }
            });
            index = 0;
        }
        
        public void addNum(int num) {
            if(index % 2 == 0){
                minHeap.offer(num);
                maxHeap.offer(minHeap.poll());
            }
            else{
                maxHeap.offer(num);
                minHeap.offer(maxHeap.poll());
            }
            index++;
        }
        
        public double findMedian() {
            double res = 0;
            if(index % 2 == 0){
                res = (minHeap.peek() + maxHeap.peek()) / 2.0;
                return res;
            }
            else{
                res =  maxHeap.peek();
                return res;
            }
        }
        private PriorityQueue<Integer> minHeap;
        private PriorityQueue<Integer> maxHeap;
        private int index;
    }
  • 相关阅读:
    CF521D Shop
    AGC033D Complexity
    CF576D Flights for Regular Customers
    LG4781 【模板】拉格朗日插值
    洛谷3288 SCOI2014方伯伯运椰子(分数规划+spfa)
    洛谷4606 SDOI2018战略游戏(圆方树+虚树)
    洛谷4630APIO2018铁人两项(圆方树+dp)
    CF487E Tourists + 圆方树学习笔记(圆方树+树剖+线段树+multiset)
    CF193D Two Segments (线段树+dp)(外加两个扩展题)
    洛谷4322 SHOI2014 三叉神经树(LCT+思维)
  • 原文地址:https://www.cnblogs.com/silentteller/p/12156415.html
Copyright © 2011-2022 走看看