常规方法 超时

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. web.xml中在Servlet中获取context-param和init-param内的参数

    引自:http://blog.csdn.net/yakson/article/details/9203231 web.xml里面可以定义两种参数:1.application范围内的参数,存放在serv ...

  2. 执行时关闭标识位 FD_CLOEXEC 的作用

    首先先回顾 apue 中对它的描述: ① 表示描述符在通过一个 exec 时仍保持有效(书P63,3.14节 fcntl 函数,在讲 F_DUPFD 时顺便提到) ② 对打开文件的处理与每个描述符的执 ...

  3. Java_DOM创建XML

    import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.FileOutputStream; ...

  4. dedecms qq咨询平均分配

    qq后台页: qq_admin.php <style type="text/css"> <!-- * {margin:0; padding:0;} .wrap { ...

  5. Dapper学习笔记(5)-存储过程

    一.无参存储过程 第一步:创建一个不带参数的存储过程,代码如下: CREATE PROCEDURE [dbo].[QueryRoleNoParms] AS BEGIN SELECT * FROM T_ ...

  6. freeCAD预选项编辑器

    freeCAD的预选项系统在 Edit 目录 -> Preferences. freecad的功能分成不同的模块,每一模块负责一个特定的工作台工作.freecad还使用了一个概念叫晚加载,这意味 ...

  7. Fiddler-2 Fiddler抓包原理

    1 fiddler抓包是在 客户端和服务器之间建立一个代理服务器,监听本机发出的请求和服务器返回的响应结果. 截一张官网的图: 2 启动fiddler之前,[dinghanhua]先来看一下代理服务器 ...

  8. 第9章 硬件抽象层:HAL

    HAL(硬件抽象层)是建立在Linux驱动之上的一套程序库.这套程序库并不属于Linux内核,而是属于Linux内核层之上的应用层.在传统的Linux系统中Linux驱动一般有两种类型的代码:访问硬件 ...

  9. What is the DD in java web application

    http://docs.oracle.com/cd/E13222_01/wls/docs70/webapp/webappdeployment.html

  10. Difference between web server ,web container and application server

    In Java: Web Container or Servlet Container or Servlet Engine : is used to manage the components lik ...