LeetCode OJ: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 data structure that supports the following two operations:
- void addNum(int num) - Add a integer number from the data stream to the data structure.
- double findMedian() - Return the median of all elements so far.
For example:
add(1)
add(2)
findMedian() -> 1.5
add(3)
findMedian() -> 2
如上,可以维护一个最大堆以及一个最小堆,如果小堆数的数量大于大堆数量。那么可以把小堆中的最小值返还给大堆。(注意这个值相当于大堆中的最大者,因为小堆中的值都是大堆中转移过去的)。代码如下:
class MedianFinder {
public:
// Adds a number into the data structure.
void addNum(int num) {
maxHeap.push(num);
int tmp = maxHeap.top();
maxHeap.pop();
minHeap.push(tmp);
if(minHeap.size() > maxHeap.size()){
tmp = minHeap.top();
minHeap.pop();
maxHeap.push(tmp);
}
}
// Returns the median of current data stream
double findMedian() {
int m = maxHeap.size();
int n = minHeap.size();
if(m > n)
return maxHeap.top()/1.0;
else
return (maxHeap.top() + minHeap.top())/2.0;
}
private:
priority_queue<int, vector<int>, greater<int>> maxHeap;
priority_queue<int, vector<int>, less<int>> minHeap;
};
LeetCode OJ:Find Median from Data Stream(找数据流的中数)的更多相关文章
- [LeetCode] 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 ...
- [LeetCode] 295. Find Median from Data Stream ☆☆☆☆☆(数据流中获取中位数)
295. Find Median from Data Stream&数据流中的中位数 295. Find Median from Data Stream https://leetcode.co ...
- [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 ...
- leetcode@ [295]Find Median from Data Stream
https://leetcode.com/problems/find-median-from-data-stream/ Median is the middle value in an ordered ...
- [leetcode]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 ...
- LeetCode——295. Find Median from Data Stream
一.题目链接: https://leetcode.com/problems/find-median-from-data-stream 二.题目大意: 给定一段数据流,要求求出数据流中的中位数,其中数据 ...
- [LeetCode] 346. Moving Average from Data Stream 从数据流中移动平均值
Given a stream of integers and a window size, calculate the moving average of all integers in the sl ...
- 剑指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
Find Median from Data Stream Median is the middle value in an ordered integer list. If the size of t ...
随机推荐
- 论文笔记:蒸馏网络(Distilling the Knowledge in Neural Network)
Distilling the Knowledge in Neural Network Geoffrey Hinton, Oriol Vinyals, Jeff Dean preprint arXiv: ...
- mellanox RDMA RoCE
一:首先根据系统发行版本下载对应的驱动,下载地址如下: http://www.mellanox.com/page/products_dyn?product_family=26&mtag=lin ...
- C/C++结构体总结
1 #include"iostream" 2 using namespace std; 3 4 struct TestStruct 5 { 6 ...
- 同时执行2条不同sql
select * from a: select *from b; 用分号结束.
- React 常用插件库
js 加密 crypto-js (des加密,md5) crypto-js https://www.npmjs.com/package/crypto-js Mock联调 数据是前端开发过程中必不可少的 ...
- JavScript 日期格式化
JavScript 日期格式化 //日期格式化 function formatDate(date,fmt) { if(date == null || typeof (date) == undefine ...
- 一键安装lnmp(2)
all(){path=`pwd`cd $pathechoecho "exclude=*.i386 *.i686" >> /etc/yum.confrpm -ivh ht ...
- mysql left join中where和on条件的区别
left join中关于where和on条件的几个知识点: 1.多表left join是会生成一张临时表,并返回给用户 2.where条件是针对最后生成的这张临时表进行过滤,过滤掉不符合where条件 ...
- 20145216史婧瑶《Java程序设计》第一周学习总结
20145216 <Java程序设计>第1周学习总结 教材学习内容总结 第一章 Java平台概论 1.1 Java不只是语言 1.Java三大平台:Java SE.Java EE与Java ...
- openssl 编译
不要费事编译了,直接下载吧! https://www.npcglib.org/~stathis/blog/precompiled-openssl/ 下载 openssl https://www.ope ...