【LeetCode】295. Find Median from Data Stream 解题报告(C++)
作者: 负雪明烛
id: fuxuemingzhu
个人博客: http://fuxuemingzhu.cn/
题目地址: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.
For example,
[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.
Example:
addNum(1)
addNum(2)
findMedian() -> 1.5
addNum(3)
findMedian() -> 2
Follow up:
- If all integer numbers from the stream are between 0 and 100, how would you optimize it?
- If 99% of all integer numbers from the stream are between 0 and 100, how would you optimize it?
题目大意
求一个数据流的中位数。
解题方法
大根堆+小根堆
让我们找到一个无线数据流中的中位数。心路历程如下:
- 我们如果能排序,就能找到中位数 ==> 排序时间复杂度太高,不可
- 把数据集划分成两部分,一半比中位数小,一半比中位数大 ==> 数据分为两部分
- 只需要知道比中位数小的那部分的最大值和比中位数大的那部分的最小值 ==> 大根堆和小根堆
所以,使用了两个堆:lesser表示比中位数小的那部分,因为要找出这部分的最大值,所以需要是大根堆;larger表示比中位数大的那部分,因为要找出这部分的最小值,所以需要时小根堆。
约定:如果数据流长度是偶数,则lesser的数字个数和larger相等;如果数据流长度是奇数,则多余的那个数字放到lesser中。即lesser.size() - larger.size() <= 1。
每个数字进来的时候,先放入lesser中,把lesser中的最大值拿出来放到larger中,此时larger会和less一样多,或者larger比lesser多一个。当larger比lesser多一个时,把larger中的最小值拿出来放到lesser中,从而保证lesser.size() - larger.size() <= 1;。
如果lesser和larger两者数据个数相等,则中位数是lesser中的最大值和larger中的最小值的平均值;如果lesser比larger多一个,那么中位数是lesser中的最大值。
注意C++中,priority_queue默认是大根堆,小根堆的定义方法是priority_queue<double, vector<double>, greater<double>>
。
C++代码如下:
class MedianFinder {
public:
/** initialize your data structure here. */
MedianFinder() {
}
void addNum(int num) {
lesser.push(num);
larger.push(lesser.top());
lesser.pop();
if (larger.size() > lesser.size()) {
lesser.push(larger.top());
larger.pop();
}
}
double findMedian() {
return lesser.size() == larger.size() ? (lesser.top() + larger.top()) / 2 : lesser.top();
}
private:
// 存放比中位数小的数字,大根堆
priority_queue<double> lesser;
// 存放比中位数大的数字,小根堆
priority_queue<double, vector<double>, greater<double>> larger;
};
/**
* Your MedianFinder object will be instantiated and called as such:
* MedianFinder* obj = new MedianFinder();
* obj->addNum(num);
* double param_2 = obj->findMedian();
*/
参考资料:https://www.cnblogs.com/grandyang/p/4896673.html
日期
2019 年 9 月 15 日 —— 中秋假期的最后一天啦,刷题加油~
【LeetCode】295. Find Median from Data Stream 解题报告(C++)的更多相关文章
- [LeetCode] 295. Find Median from Data Stream ☆☆☆☆☆(数据流中获取中位数)
295. Find Median from Data Stream&数据流中的中位数 295. Find Median from Data Stream https://leetcode.co ...
- 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数据流的中位数
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 二.题目大意: 给定一段数据流,要求求出数据流中的中位数,其中数据 ...
- 剑指offer 最小的k个数 、 leetcode 215. Kth Largest Element in an Array 、295. Find Median from Data Stream(剑指 数据流中位数)
注意multiset的一个bug: multiset带一个参数的erase函数原型有两种.一是传递一个元素值,如上面例子代码中,这时候删除的是集合中所有值等于输入值的元素,并且返回删除的元素个数:另外 ...
- 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 ...
- [LC] 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 ...
- 295 Find Median from Data Stream 数据流的中位数
中位数是排序后列表的中间值.如果列表的大小是偶数,则没有中间值,此时中位数是中间两个数的平均值.示例:[2,3,4] , 中位数是 3[2,3], 中位数是 (2 + 3) / 2 = 2.5设计一个 ...
随机推荐
- pheatmap() 的热图制作
1.数据准备 2.画图 3.参数调整 (转自百迈克公众号) 关注下方公众号可获得更多精彩
- Python三元表达式,列表推导式,字典生成式
目录 1. 三元表达式 2. 列表推导式 3. 字典生成式 3.1 字典生成式 3.2 zip()方法 1. 三元表达式 """ 条件成立时的返回值 if 条件 else ...
- Dango之form校验组件
目录 1.引入案例 2. form组件的功能 3. form组件的使用 3.1 自定义form校验类 3.2 校验数据 3.3 渲染页面 3.4 展示错误信息 3.5 自定义校验结果 3.6 form ...
- Qt最好用评价最高的是哪个版本?
来源: http://www.qtcn.org/bbs/read-htm-tid-89455.html /// Qt4: 4.8.7 4.X 系列终结版本 Qt5 : 5.6 LT ...
- Scala(二)【基本使用】
一.变量和数据类型 1.变量 语法:val / var 变量名:变量类型 = 值 val name:String = "zhangsan" 注意 1.val定义的变量想到于java ...
- echarts饼图样式
1.中间标题字体大小不一致(可分为一个title一个graphic) 2.labelLine与饼图分离(两个饼图,其中一个显示一个隐藏) function setmyChartJsgxzq(arr,d ...
- 容器之分类与各种测试(三)——queue
queue是单端队列,但是在其实现上是使用的双端队列,所以在queue的实现上多用的是deque的方法.(只要用双端队列的一端只出数据,另一端只进数据即可从功能上实现单端队列)如下图 例程 #incl ...
- Postman 中 Pre-request Script 常用 js 脚本
1. 生成一个MD5或SHA1加密的字符串str_md5,str_sha1 string1 = "123456"; var str_md5= CryptoJS.MD5(string ...
- Oracle trunc和round的区别
1.关于trunc 和round函数比较 整体概括: round函数 四舍五入trunc函数 直接截取 对于时间: Round函数对日期进行"四舍五入",Trunc函数对日期进行截 ...
- vue引入d3
单页面使用 cnpm install d3 --save-dev 指定版本安装 cnpm install d3@6.3.1 -S <script> import * as d3 from ...