Find Median from Data Stream

常规方法 超时

class MedianFinder {
    vector<int> coll;
public:
    MedianFinder(){

    }
    void heapfu(vector<int>& coll,int idx,int max){
        int left=2*idx+1,right=2*idx+2;
        int largest=idx;
        if(left<max&&coll[left]>coll[idx])  largest=left;
        if(right<max&&coll[largest]<coll[right])  largest=right;
        if(largest!=idx){
            swap(coll[largest],coll[idx]);
            heapfu(coll,largest,max);
        }
    }
    // Adds a number into the data structure.
    void addNum(int num) {
        coll.push_back(num);
        swap(coll[0],coll[coll.size()-1]);
        for(int i=coll.size()/2-1;i>=0;i--)
            heapfu(coll,i,coll.size());
    }

    // Returns the median of current data stream
    double findMedian() {
        if(coll.size()==2) return (double)(coll[0]+coll[1])/2;
        if(coll.size()==0)  return 0;
        if(coll.size()==1)  return coll[0];
        if(coll.size()%2 == 0){
            for(int i=0;i<=coll.size()/2-1;i++){
                swap(coll[0],coll[coll.size()-1-i]);
                heapfu(coll,0,coll.size()-i-1);
            }
            int a=coll[0];
            swap(coll[0],coll[coll.size()-1-coll.size()/2-1]);
            heapfu(coll,0,coll.size()-coll.size()/2-1-1);
            return (double)(a+coll[0])/2;
        }
        else{
            for(int i=0;i<=coll.size()/2;i++){
                swap(coll[0],coll[coll.size()-1-i]);
                heapfu(coll,0,coll.size()-i-1);
            }
            return (double)coll[0];
        }
    }
};

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

两个优先队列  数组如果是 123 789

这样存储 3,2,1  和 7,8,9

class MedianFinder {
    priority_queue<int, vector<int>, greater<int>> min_heap;
    priority_queue<int, vector<int>, less<int>> max_heap;
public:
    // Adds a number into the data structure.
    void addNum(int num) {
        if(min_heap.empty()||num>min_heap.top())
            min_heap.push(num);
        else
            max_heap.push(num);
        if(min_heap.size()>max_heap.size()+1){
            max_heap.push(min_heap.top());
            min_heap.pop();
        }
        else if(max_heap.size()>min_heap.size()){
            min_heap.push(max_heap.top());
            max_heap.pop();
        }
    }

    // Returns the median of current data stream
    double findMedian() {
        return min_heap.size()==max_heap.size()? 0.5*(min_heap.top()+max_heap.top()):min_heap.top();
    }
};

Q:如果要求第n/10个数字该怎么做?
A:改变两个堆的大小比例,当求n/2即中位数时,两个堆是一样大的。而n/10时,说明有n/10个数小于目标数,9n/10个数大于目标数。所以我们保证最小堆是最大堆的9倍大小就行了。

时间: 2024-07-30 11:29:42

Find Median from Data Stream的相关文章

[LeetCode] Find Median from Data Stream

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

leetcode笔记: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 De

支线任务8-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 Des

LeetCode之Find Median from Data Stream

1.题目描述 翻译过来就是对输入的数字找出其中位数. 2.解题思路 一开始,我想着这是动态输入,所以必须放弃数组这些静态的数据结构.首先想到了平衡二叉树,然而太麻烦了.后面又想到了大顶堆以及小顶堆,但是不知如何运用,就上这道题discuss瞅了瞅.结果发现了一种double heap解法,十分带感,总体思路就是比中位数大的数字放一边(大数堆),小的放另一边(小数堆).然后也接触了并且运用一种新的队列用法——优先队列.插入数字过程中,始终保持大数堆大小大于等于小数堆,以便最后输出中位数.代码如下:

数据结构与算法(1)支线任务8——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] 

解题报告:295. 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 Desi

295. 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 d

[LeetCode] 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 d

[email&#160;protected] [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 m