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倍大小就行了。
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 t ...
- [LeetCode] 295. Find Median from Data Stream ☆☆☆☆☆(数据流中获取中位数)
295. Find Median from Data Stream&数据流中的中位数 295. Find Median from Data Stream https://leetcode.co ...
- 剑指offer 最小的k个数 、 leetcode 215. Kth Largest Element in an Array 、295. Find Median from Data Stream(剑指 数据流中位数)
注意multiset的一个bug: multiset带一个参数的erase函数原型有两种.一是传递一个元素值,如上面例子代码中,这时候删除的是集合中所有值等于输入值的元素,并且返回删除的元素个数:另外 ...
- [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 ...
- 数据结构与算法(1)支线任务8——Find Median from Data Stream
题目如下:(https://leetcode.com/problems/find-median-from-data-stream/) Median is the middle value in an ...
- 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 ...
- 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 ...
- leetcode@ [295]Find Median from Data Stream
https://leetcode.com/problems/find-median-from-data-stream/ Median is the middle value in an ordered ...
- 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 ...
随机推荐
- Oracle查询数据库中的所有表
SELECT A.TABLE_NAME 表英文名, A.TAB_COMMENTS 表中文名, A.COLUMN_ID 序号, A.COLUMN_NAME 英文名, ...
- a链接的四种状态
四种状态:hover.link.active.visible a:link{color:#fff;} a链接初始化 a:hover{color:yellow;} 把鼠标放上去的状况 a:active{ ...
- a标签
a链接是一种触发行为元素,行内元素 属性:href/target/class/id/title href: <a href="www.baidu.com"></a ...
- iOS架构师之路:控制器(View Controller)瘦身设计
前言 古老的MVC架构是容易被iOS开发者理解和接受的设计模式,但是由于iOS开发的项目功能越来越负责庞大,项目代码也随之不断壮大,MVC的模糊定义导致我们的业务开发工程师很容易把大量的代码写到视图控 ...
- Power of Four(Difficulty: Easy)
题目: Given an integer (signed 32 bits), write a function to check whether it is a power of 4. Example ...
- php 目录函数和日期函数
continue . break . exit目录函数opendir(); 打开一个文件夹is_file 只判断文件是否存在: file_exists 判断文件是否存在或者是目录是否存在: is_di ...
- jquery实现css3动画
jquery animate改变元素样式时,只支持数字值的变化,比如width,height等,但是css3属性状态值很多都不是数字值,而是字符串和数字混合在一起,比如translate(), rot ...
- 简单阐述下OC中UIImage三种创建方式~~~
一. 直接使用imageNamed进行创建 UIImage * image = [UIImage imageNamed:@"1.jpg"]; 简单说一下这种方式的优缺点: 优点:代 ...
- [解决方案] pythonchallenge level 6
查看页面代码,知道找zip www.pythonchallenge.com/pc/def/channel.zip,查看zip下的readme.txt知道从90052,跑一遍知道要收集zip的comme ...
- WPF(WP7、WP8)多个Listbox嵌套时滚动问题的解决
内部的ListBox加属性 ScrollViewer.VerticalScrollBarVisibility="Disabled" 即可 如果不需要滚动,可以考虑嵌套换成 Item ...