Find Median from Data Stream - LeetCode

Find Median from Data Stream - LeetCode

题目
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:
1.void addNum(int num) - Add a integer number from the data stream to the data structure.
2.double findMedian() - Return the median of all elements so far.


这道题要我们找中位数。看到题目后有两种想法,第一种是插入的时候保持顺序,然后找中位数时根据数组长度算出中位数;第二种始终记录中位数的位置,插入的时候保持顺序,并且更改指向中位数的指针,findMedian()时只需根据指针,以及数组元素个数的奇偶来判断。

实际尝试下来第二种效率更高

class MedianFinder {
public:
    /** initialize your data structure here. */
    MedianFinder() {
        it = myset.end();
        isodd = false;
    }

    void addNum(int num) {
        myset.insert(num);
        if (it == myset.end()) {
            it = myset.begin();
            isodd = true;
        } else {
            if (*it > num && isodd == true) {
                it--;
            } else if(*it <= num && isodd == false) {
                it++;
            }
            isodd = !isodd;
        }

    }

    double findMedian() {
        if (isodd) {
            return *it;
        } else {
            double a =*it, b = *++it;
            --it;
            return (a+b)/2;
        }
    }
private:
    multiset<int> myset;
    set<int>::iterator it;
    bool isodd;
};

再看看别人的高大上代码

class MedianFinder {
public:
    priority_queue<int> loHalf, upHalf;


    // Adds a number into the data structure.
    void addNum(int num) {

        if(loHalf.empty() || num < loHalf.top()) loHalf.push(num);
        else upHalf.push(-num);

        if(loHalf.size() >   1 + upHalf.size()) {
            upHalf.push(-loHalf.top());
            loHalf.pop();
        }
        else if(upHalf.size() >   1 + loHalf.size()) {
            loHalf.push(-upHalf.top());
            upHalf.pop();
        }

    }

    // Returns the median of current data stream
    double findMedian() {
        if(loHalf.size() > upHalf.size()) return loHalf.top();
        else if(upHalf.size() > loHalf.size()) return -upHalf.top();
        else return (loHalf.top() - upHalf.top())/2.0;
    }
};

这个方法是把这组数分成两部分lowhalf和uphalf,每次插入后调整这两部分的元素个数,使他们元素个数差不超过1,这样在找中位数时就能根据奇偶和两部分的大小来得出

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值