【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设计一个 ...
随机推荐
- nginx_update
软件下载 预编译 编译 配置 [root@MiWiFi-R1CM-srv ~]#wget -c https://nginx.org/download/nginx-1.15.0.tar.gz 通过-V查 ...
- A Child's History of England.14
At first, Elfrida possessed great influence over the young King, but, as he grew older and came of a ...
- day9 图书设计项目
总路由层url from django.conf.urls import url from django.contrib import admin from app01 import views ur ...
- C语言大小端判定
要判定大小端?需要弄清以下几个问题: 1.当一个变量占多个字节时,变量的指针指向的是低地址 2.什么是大小端? 大端模式:是指数据的高字节保存在内存的低地址中,而数据的低字节保存在内存的高地址中. 小 ...
- 【leetcode】153. Find Minimum in Rotated Sorted Array
Suppose an array of length n sorted in ascending order is rotated between 1 and n times. For example ...
- Qt——error之undefined reference to `vtable for classname
可能原因:自定义类中使用自定义槽和信号,但是没有在类中增加Q_OBJECT, 解决办法:在类中增加Q_OBJECT,删除编译产生的文件进行重新编译 具体原因分析如下 博主原文
- C语言time函数获取当前时间
以前放了个链接,但是原作者把博文删了,这里放一个获取时间的代码,已经比较详细所以不做注释 #include<stdio.h> #include<time.h> #include ...
- webservice--cxf和spring结合
服务端: 实体: package entity; import java.util.Date; /*** 实体 */ public class Pojo { //温度 private String d ...
- android 获取uri的正确文件路径的办法
private String getRealPath( Uri fileUrl ) { String fileName = null; if( fileUrl != null ) { if( file ...
- Undefined symbols for architecture arm64:问题
Undefined symbols for architecture arm64: "_sqlite3_prepare_v2", referenced from: +[HMJSch ...