LeetCode——295. Find Median from Data Stream
一.题目链接:
https://leetcode.com/problems/find-median-from-data-stream
二.题目大意:
给定一段数据流,要求求出数据流中的中位数,其中数据流是动态变化的。如果数据流中的数字个数是奇数的话,则中位数是中间位置的数字;如果数据流中的数字是偶数的话,则中位数是排序好的数据流中的中间两个数的的平均值。
三.题解:
如果数据流是静态不变的话,此时问题是比较好求解的。但是数据流是动态变化的,所以数据流中每次进入一个新的数字时,都要保证能够高效的找到数据流的中位数。我们可以这么考虑:如果把数据流中的数字分为个数相同的两部分的话(假设为A和B,其中A中的数字全部小于B中的数字),那么我们所求的中位数,实质就是A中的最大值和B中的最小值的平均值。因此,我们可以用两个堆来表示这个过程,其中大顶堆maxH存储的是数据流中的数值较小的数字,而小顶堆minH存储的是数据六中数值较大的数字,且maxH中的数字全部小于minH中的数字。并且堆可以快速的找出其中的最值,所以可以快速找得到minH中的最小值和maxH中的最大值,从而求出中位数。代码如下:
#include<iostream>
#include<unordered_map>
#include<queue>
#include<cstring>
#include<cstdlib>
#include<cmath>
#include<algorithm>
#include<sstream>
#include<set>
#include<map>
#include<stack>
#define MAX_NUM 100
using namespace std;
class MedianFinder {
public:
/** initialize your data structure here. */
priority_queue<int,vector<int>,less<int>> maxH;//定义大顶堆,用于存储较小的数据部分
priority_queue<int,vector<int>,greater<int>> minH;//定义小顶堆,用于存储较大的数据部分
MedianFinder() { } void addNum(int num) {
maxH.push(num);
int tmp = maxH.top();
maxH.pop();
minH.push(tmp);//保证小顶堆中的数据大于大顶堆中的数据
if(minH.size() > maxH.size())
{
int tmp = minH.top();
minH.pop();
maxH.push(tmp);//保证大顶堆中的数据小于小顶堆中的数据
} } double findMedian() {
if(minH.size() == maxH.size())//如果两个堆的大小相同,则返回它们最值的平均值
return (minH.top() + maxH.top()) / 2.0;
return minH.size() > maxH.size() ? minH.top() : maxH.top();//如果两个堆的大小不相同,返回数字个数多的堆的最值
}
}; /**
* Your MedianFinder object will be instantiated and called as such:
* MedianFinder obj = new MedianFinder();
* obj.addNum(num);
* double param_2 = obj.findMedian();
*/ int main()
{
MedianFinder test = MedianFinder();
test.addNum(1);
test.addNum(2);
test.addNum(3);
test.addNum(4);
test.addNum(5);
cout<<test.findMedian()<<endl; }
每次从数据流中找出中位数的时间为O(1),调整两个堆的时间为O(logN),对于含有n个数字的数据流,总的时间复杂度为O(NlogN),空间复杂度为O(N)。
此外,有几个点需要注意下:
1.代码中使用了priority_queue即优先队列来实现堆,因为优先队列获取优先级最高的值所需时间为O(1),调整的过程为O(logN),与堆的操作时间类似,能较好的模拟堆。
2.通常情况下,堆默认的优先级最高的值是指的最大值,也可以是最小值,不过需要显式的说明(见本例中优先队列的定义)。
3.要保证最小堆中存储的始终是较大的数值,而大顶堆中存储的是较小的数值。所以才会有addNum中的那些操作。
LeetCode——295. 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 ...
- 剑指offer 最小的k个数 、 leetcode 215. Kth Largest Element in an Array 、295. Find Median from Data Stream(剑指 数据流中位数)
注意multiset的一个bug: multiset带一个参数的erase函数原型有两种.一是传递一个元素值,如上面例子代码中,这时候删除的是集合中所有值等于输入值的元素,并且返回删除的元素个数:另外 ...
- 【LeetCode】295. Find Median from Data Stream 解题报告(C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 大根堆+小根堆 日期 题目地址:https://le ...
- 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设计一个 ...
随机推荐
- json模块
dic = {"name":"boke","age":"18"} #字典 data = json.dumps(dic) ...
- MySQL 相关记录
删除courseID_tr之前: 新建之后: show create trigger: 下面是mysql-5.7.21-win64解压目录下的my.ini文件中的内容 [client] default ...
- PHP错误日志和内存查看(转)
本篇文章给大家带来的内容是关于PHP错误日志和内存查看的方法介绍(代码),有一定的参考价值,有需要的朋友可以参考一下,希望对你有所帮助. 1.通过命令查看服务器上一共开了多少的 php-cgi 进程: ...
- git配置代理
1.查看当前代理 git config --global http.proxy 2.配置git走代理,可走http代理也可以走socks5代理,可根据自己的代理协议而定 #http代理 git con ...
- 阿里云 oss 图片上传解决方案 vue (web直传)
我们通过aliyun-oss-web这个npm去解决 该文章主要介绍如何获取 imgSignature 和 imgPolicy 这两个参数 首先下载 web直传的案例 : http://files.c ...
- linux文件查找find命令
linux文件查找find命令 1.文件查找 基本介绍 在文件系统上查找符合条件的文件 linux上常见的文件查找工具:find命令 查找分类 实时查找 精确查找 基本语法 find [option ...
- 项目里如何访问AppDelegate
项目里面访问AppDelegate做全局变量用有好几种方式 最原始就是 AppDelegate *appDelegate = (AppDelegate *)[[UIApplication shared ...
- useful urls
数据挖掘技术: http://ddl.escience.cn/f/IwoF?rid=8188575 李航 统计学习方法: http://ddl.escience.cn/f/Iwn0
- Build up java environment(配置java环境)
1,配置环境变量 我的电脑,右键计算机图标,点击“属性” 点击“高级系统设置” 点击“环境变量” “系统变量”一栏,点击“新建” 弹出输入“变量名”.“变量值”窗口 “变量名”输入“JAVA_HOME ...
- 解决win7无法运行bat批处理文件的方法
在win7系统中我们可以将一些命令制作为bat批处理文件,只需双击打开即可运行命令,方便使用. 那么,要怎么运行bat批处理呢?最近有用户反馈,遇到无法运行bat批处理的现象,该怎么办呢? 修复方法一 ...