常规方法 超时

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倍大小就行了。

Find Median from Data Stream的更多相关文章

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

  2. [LeetCode] 295. Find Median from Data Stream ☆☆☆☆☆(数据流中获取中位数)

    295. Find Median from Data Stream&数据流中的中位数 295. Find Median from Data Stream https://leetcode.co ...

  3. 剑指offer 最小的k个数 、 leetcode 215. Kth Largest Element in an Array 、295. Find Median from Data Stream(剑指 数据流中位数)

    注意multiset的一个bug: multiset带一个参数的erase函数原型有两种.一是传递一个元素值,如上面例子代码中,这时候删除的是集合中所有值等于输入值的元素,并且返回删除的元素个数:另外 ...

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

  5. 数据结构与算法(1)支线任务8——Find Median from Data Stream

    题目如下:(https://leetcode.com/problems/find-median-from-data-stream/) Median is the middle value in an ...

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

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

  8. leetcode@ [295]Find Median from Data Stream

    https://leetcode.com/problems/find-median-from-data-stream/ Median is the middle value in an ordered ...

  9. Find Median from Data Stream 解答

    Question Median is the middle value in an ordered integer list. If the size of the list is even, the ...

随机推荐

  1. CSS布局学习笔记之position

    CSS知识点 之 position布局 前段时间被同学怂恿,参加了百度前端技术学院的一个小培训,第一个阶段下来学到不少东西.课程的第一个阶段主要是HTML5 和 CSS 基础知识的一个小培训,给出的一 ...

  2. CnBlog客户端Windows Live Write安装方法

    官方帮助http://space.cnblogs.com/forum/topic/8550 注:如果自动配置没有成功,需要手动配置: a) 在"Type of  weblog that yo ...

  3. centos 6 安装 gitlib

    安装gitlab-----------1. 下载 gitlabcurl -O https://downloads-packages.s3.amazonaws.com/centos-6.5/gitlab ...

  4. canvas标签(1)--线条、矩形、圆形、文本、阴影、抛小球

    从网上扒拉的代码,敲了敲代码玩. html页面显示内容很简单,只要写个canvas标签,给他一个id写js就可以了 <!DOCTYPE html> <html> <hea ...

  5. 第六篇——初尝Python,意犹未尽

    作业2的要求是选一个你从来没有学过的编程语言,试一试实现基本功能.那么在这里我准备学习Python语言进行学习,并尝试用Python写一写东西. http://www.runoob.com/ Pyth ...

  6. .NET3.5项目转.NET2.0项目技巧

    最近有一个项目,一开始开发是用VS2008(.NET3.5)开发的,该项目是一个Windorm客户端软件,由于在大规模的推广过程中,发现在安装.NET3.5Framework的时候浪费了太多时间,而且 ...

  7. TypeScript 中的 "=>" 真的很好用!!!

    class funs { public $scope: IBarPadScope; constructor($scope: IBarPadScope) { this.$scope = $scope; ...

  8. win7 32 bit VS2012 OpenCV3.0配置

    今天看CPP基础,想起来之前在vs2012配置opencv3未成功,就忍不住再次配置一... 环境:win7 32bit vs2012 opencv3.0 主要参考这几篇博文:1,2,3 上面的博文已 ...

  9. C/C++读入一行不定个数的整数

    我想,每个人一开始遇到这个问题,都会觉得挺简单的.但真正实施的时候,可能就会觉得还是有点坑的.毕竟对于C/C++这样成熟而使用广泛的语言而言,对于这个简单的问题竟然没有一个简洁有力甚至一行代码的解决方 ...

  10. Win10/UWP新特性—Drag&Drop 拖出元素到其他App

    在以前的文章中,写过微软新特性Drag&Drop,当时可能由于处于Win10预览版,使用的VS也是预览版,只实现了从桌面拖拽文件到UWP App中,没能实现从UWP拖拽元素到Desktop A ...