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设计一个 ...
随机推荐
- maven 常用备忘录
1.maven 国内的常用中央仓库地址配置: <mirror> <id>alimaven</id> <name>aliyun maven</nam ...
- DevOps 开源工具
1. 开发工具 版本控制&协作开发 版本控制系统 Git Git 是一个开源的分布式版本控制系统,用以有效.高速的处理从很小到非常大的项目版本管理.开源中国 Git 代码托管平台:http:/ ...
- HAProxy原理和配置
HAProxy原理和配置 目录 1.HAProxy简介 2.haproxy安装和配置说明 proxies配置参数 bind配置 Balance配置 基于cookie的会话绑定 统计接口启用相关的参数 ...
- Windows10 VS2017 C++编译Linux程序
#include <cstdio> #include <iostream> #include "unistd.h" using namespace std; ...
- ios 视图既显示阴影又有圆角实现
//- (UIView *)createTimeBG //{ // UIView *view = [[UIView alloc]init]; // view.backgroundColor ...
- 微软连续12年成为Gartner分析和BI平台魔力象限的领导者
小悦还沉浸在新春开工大吉的工作中,微软Power BI就又迎来了一个好消息!据Gartner刚新鲜出炉的< 2019年Gartner的分析和商业智能平台魔力象限报告>,微软迄今已连续12 ...
- 配置trunk
SW1 Switch>en Switch#conf t Enter configuration commands, one per line. End with CNTL/Z. Switch(c ...
- [LeetCode&Python] Problem 70. Climbing Stairs
You are climbing a stair case. It takes n steps to reach to the top. Each time you can either climb ...
- UML第二次作业:类在类图中的表示
类在类图中的表示 一.概览 1.plant UML语法学习小结 2.班级学生管理系统中的 —— “学生” 类的属性.方法 3.类图脚本程序 4.类图示例 二.类图语法学习小结 1.类之间的关系 通过某 ...
- 30天代码day0
a class is a collection of variables (fields) and functions called methods. A program is a collectio ...