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 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
二. 题目分析
题目大意是,在一串输入的数据流中寻找中位数。所谓中位数。就是指有序整数列表的中间值。
假设列表的长度为偶数,此时没有中间值。则当中位数就是两个中间值的平均值。
比如:
[2,3,4]
, 中位数是 3
[2,3]
, 中位数是 (2 + 3) / 2 = 2.5
设计一种数据结构,支持以下两种操作:
void addNum(int num) // 该函数从数据流向数据结构增加一个整数
double findMedian() // 返回截至眼下全部元素的中位数
该题的经典做法是。维护一个最大堆和一个最小堆。最大堆存的是截至眼下为止较小的那一半数,最小堆存放的是截至眼下为止较大的那一半数。这样中位数仅仅有可能是堆顶或者两个堆顶所相应两个数的平均值。
维护两个堆的技巧在于推断堆顶端的数和新插入的数的大小关系,另外,因为两个堆平分了全部数。因此还须要考虑两者的大小关系。这里我们规定两个堆的大小之差不超过1
。先推断堆顶数和新数的大小关系,有以下三种情况:
- 最小堆堆顶小于新插入的数时。说明新插入的数处在全部数的上半部分;
- 最大堆堆顶大于新插入的数时,说明新插入的数处在全部数的下半部分;
- 最小堆堆顶大于新插入的数时,但最大堆堆顶小于新插入的数时,说明新插入的数将处在最小堆堆顶或最大堆堆顶,也就是在中间的位置。
再推断两个堆的大小关系,假设新插入的数属于前两种情况,開始插入目标堆,此时又有两种操作:
- 若目标堆不大于还有一个堆时,将新数插入目标堆;
- 若目标堆大于还有一个堆时,将目标堆的堆顶先移动到还有一个堆。再把新数插入目标堆。
假设新插入的数属于第三种情况。即在中间位置。则插入到大小较小的那个堆就可以。
这样,每次新加进来一个数以后。若两个堆一样大,则中位数是两个堆顶的平均值。否则较大的那个堆的堆顶为中位数。
建立两个堆所用的代码比較长,而使用优先队列来实现则简单很多。
priority_queue:优先队列,是一个拥有权值概念的单向队列。在这个队列中,全部元素是按优先级排列的。优先队列有两种。一种是最大优先队列;一种是最小优先队列;每次取自队列的第一个元素各自是优先级最大和优先级最小的元素。
实际使用时,增加头文件:"queue.h"
, "functional.h"
当中”functional.h”定义了优先级。(若要自定义优先级能够不加)关于优先队列的使用可參照:
http://www.cnblogs.com/summerRQ/articles/2470130.html
http://blog.csdn.net/zhang20072844/article/details/10286997
三. 演示样例代码
class MedianFinder
{
private:
priority_queue<int,std::vector<int>, std::greater<int>> q1; // 数据越小,优先级越高
priority_queue<int> q2; // 数据越大,优先级越高
public:
void addNum(int num)
{
if(q2.empty())
{
q2.push(num);
return;
}
if(num <= q2.top())
{
if(q2.size() <= q1.size()) q2.push(num);
else
{
q1.push(q2.top());
q2.pop();
q2.push(num);
}
}
else
{
if(q2.size() <= q1.size())
{
if(num <= q1.top()) q2.push(num);
else
{
q2.push(q1.top());
q1.pop();
q1.push(num);
}
}
else
{
q1.push(num);
}
}
}
double findMedian()
{
if(q1.size() == q2.size()) return (q1.top() + q2.top()) / 2.0;
return double(q2.top());
}
};
// Your MedianFinder object will be instantiated and called as such:
// MedianFinder mf;
// mf.addNum(1);
// mf.findMedian();
四. 小结
又一次认真学了一下优先队列。受益匪浅。
leetcode笔记:Find Median from Data Stream的更多相关文章
- [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函数原型有两种.一是传递一个元素值,如上面例子代码中,这时候删除的是集合中所有值等于输入值的元素,并且返回删除的元素个数:另外 ...
- [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] 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 解题报告(C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 大根堆+小根堆 日期 题目地址:https://le ...
随机推荐
- CF909B Segments
CF909B Segments 题意翻译 题目描述 给你一个整数N.考虑坐标轴上所有可能的部分,在整数点上的端点,坐标在0到N之间,包括它们. 您希望在几个层中绘制这些片段,这样在每个层中这些片段就不 ...
- six.moves的用法
six是用来兼容python 2 和 3的,我猜名字就是用的2和3的最小公倍数. six.moves 是用来处理那些在2 和 3里面函数的位置有变化的,直接用six.moves就可以屏蔽掉这些变化 P ...
- 用户体验之如何优化你的APP
用户体验,速度为王,来几个优化APP“速度”的建议. 1.后台执行 毋庸多言,已是通常做法. 一般在执行下载任务时让其在后台运营,让用户有精力去做别的事情. 后端加载 2.提前显示 客户端与WEB的数 ...
- tp中使用事务
是什么 事务是为了防止,多个操作,其中有失败,数据有部分被执行成功的时候使用的. 比如,银行,用户转账.张三钱扣了,结果李四钱还增加! 这个时候需要使用事务,确保张三钱扣了,李四的钱也增加,才真正的成 ...
- 准确率99%!基于深度学习的二进制恶意样本检测——瀚思APT 沙箱恶意文件检测使用的是CNN,LSTM TODO
所以我们的流程如图所示.将正负样本按 1:1 的比例转换为图像.将 ImageNet 中训练好的图像分类模型作为迁移学习的输入.在 GPU 集群中进行训练.我们同时训练了标准模型和压缩模型,对应不同的 ...
- isual Studio 2013编译ImageMagick---转
“该文引用自 CruiseYoung的:Visual Studio 2013编译ImageMagick http://blog.csdn.net/fksec/article/details/36008 ...
- BZOJ 3052 树上带修莫队
思路: 就是把带修莫队移到了树上 块的大小开到(n^2/3)/2 比较好- 这是一个卡OJ好题 //By SiriusRen #include <cmath> #include <c ...
- 在Android源码下编译jni所需要知道的事~
以下只是自己的一些总结,欢迎讨论 通过NDK编译jni网上有很多例子,在这我只总结在Android源码下编译 1.android源码环境下编译so包,编出来的.so的包前面不会自动给添加lib,NDK ...
- firstChild与firstElementChild
相同点: 都是获取父元素下的第一个节点对象 不同点: firstChild: IE6.7.8 第一个元素节点; 非IE6.7.8:返回第一个元素节点或文本节点 firstElementChild: I ...
- JAVA在线观看视频教程完整版
今天给大家介绍一下JAVA在线观看视频教程完整版,我们知道Java是一种可以撰写跨平台应用软件的面向对象的程序设计语言,是由Sun Microsystems公司于1995年5月推出的Java程序设计语 ...