1.题目要求

中位数是有序列表中间的数。如果列表长度是偶数,中位数则是中间两个数的平均值。

例如,

[2,3,4] 的中位数是 3

[2,3] 的中位数是 (2 + 3) / 2 = 2.5

设计一个支持以下两种操作的数据结构:

  • void addNum(int num) - 从数据流中添加一个整数到数据结构中。
  • double findMedian() - 返回目前所有元素的中位数。

示例:

  addNum(1)
addNum(2)
findMedian() -> 1.5
addNum(3)
findMedian() -> 2

进阶:

    1. 如果数据流中所有整数都在 0 到 100 范围内,你将如何优化你的算法?
    2. 如果数据流中 99% 的整数都在 0 到 100 范围内,你将如何优化你的算法?

2.解题思路

堆是一个非常重要的数据结构,堆排序在C++中的实现为优先级队列(Priority_queue),关于这一点,我的另一篇博文 "Leetcode 703. 数据流中的第K大元素"  有更详细提到,这里不做重复。

LeetCode网站把这一道划分在“堆”一类中,也是提醒我们使用堆结构。这道题很巧妙,我是听了算法课(牛客网的左程云大牛)的讲解才弄明白。这里的代码是自己听懂了思路,独立写出来的。

关键思路:建立两个堆(使用priority_queue实现),一个大根堆,一个小根堆。

(1)一个大根堆保存所有整数中较小的1/2;一个小根堆保存所有整数中较大的1/2
          (2)并且,依次添加元素过程中,两个堆元素个数的差的绝对值不能超过1

这样,两个堆建立好了以后,

(1)如果输入的元素个数 n 是偶数,则两个堆的元素个数相等,分别取大根堆的顶和小根堆的顶,取平均值,即是所求的整个数据流的中位数;

(2)如果输入的元素个数 n 是奇数,则必有一个堆的元素个数为(n/2+1),返回这个堆的顶,即为所求的中位数。

3.我的代码

个人比较喜欢写段落注释行注释,因为这样自己一年之后还能快速看懂,当然也方便他人,特别是一起刷题的伙伴,轻松看懂。

更多的细节讲解里都在注释里。如有错误的地方,欢迎多指正。

代码通过所有测试案例的时间为124ms。

class MedianFinder {
public:
/** initialize your data structure here. */
MedianFinder() { } void addNum(int num) {
/*建立两个堆:(1)一个大根堆,保存所有整数中较小的1/2;一个小根堆,保存所有整数中较大的1/2;
(2)并且,依次添加元素过程中,两个堆大小的差的绝对值不能超过1; */ //第一元素加入大根堆
if(heap1.size()==){
heap1.push(num);
return;
} if(num<=heap1.top()){
//第二个元素比大根堆的顶小
heap1.push(num); //大根堆元素过多
if(heap1.size()-heap2.size()>)
{
int temp = heap1.top();
heap1.pop();
heap2.push(temp);//大根堆弹出顶到小根堆
} }
else{
//第二个元素比大根堆的顶大,直接进入小根堆
heap2.push(num); //小根堆元素过多
if(heap2.size()-heap1.size()>)
{
int temp = heap2.top();
heap2.pop();
heap1.push(temp);//小根堆弹出顶到大根堆
}
} } double findMedian() {
//输入的元素为奇数个
if(heap1.size() > heap2.size())
return heap1.top();
else if(heap1.size() < heap2.size())
return heap2.top(); //输入的元素个数为偶数
else
return (heap1.top()+heap2.top())/2.0;
//取大根堆、小根堆的堆顶元素取平均值,即为所求全局中位数
} private:
priority_queue<int> heap1;//默认,大根堆
priority_queue<int,vector<int>,greater<int>> heap2;//小根堆(升序序列) }; /**
* Your MedianFinder object will be instantiated and called as such:
* MedianFinder obj = new MedianFinder();
* obj.addNum(num);
* double param_2 = obj.findMedian();
*/

4.用时更少的示例代码

这是我提交解答后,查看细节,看到的Leetcode官网上提交的关于这道题运行时间最短(96ms)的示例代码。

LeetCode上刷好多速度排名第一的代码中都有一段类似的代码,就是下面代码中的第一段代码——优化C++的IO速度。

/*一般地,C++的运行速度不如C的,主要原因是C++的输入输出流兼容了C的输入输出,因此,C++的速度才会变慢,
如果去掉C++的输入输出的兼容性的话,速度就和C的差不多了*/
static const auto __ = []() {
// turn off sync
std::ios::sync_with_stdio(false);
// untie in/out streams
std::cin.tie(nullptr);
return nullptr;
}(); class MedianFinder {
public:
/** initialize your data structure here. */ //使用vector实现两个堆,而不是priority_queue
vector<int> maxheap;
vector<int> minheap; bool flag = true; MedianFinder() {
} void addNum(int num) {
if(flag){
//构建小根堆
if(minheap.size()>&&num>minheap[]){
minheap.push_back(num);
push_heap(minheap.begin(),minheap.end(),greater<int>());
num = minheap[];
pop_heap(minheap.begin(),minheap.end(),greater<int>());
minheap.pop_back();
}
maxheap.push_back(num);
push_heap(maxheap.begin(),maxheap.end(),less<int>());
flag=false;
}else{
//构建大根堆
if(maxheap.size()>&&num<maxheap[]){
maxheap.push_back(num);
push_heap(maxheap.begin(),maxheap.end(),less<int>());
num = maxheap[];
pop_heap(maxheap.begin(),maxheap.end(),less<int>());
maxheap.pop_back();
}
minheap.push_back(num);
push_heap(minheap.begin(),minheap.end(),greater<int>());
flag=true;
}
} double findMedian() {
if(maxheap.size()<&&minheap.size()<)
return ;
if(flag){
return (maxheap[]+minheap[])/2.0;
}else{
return maxheap[];
}
}
}; /**
* Your MedianFinder object will be instantiated and called as such:
* MedianFinder obj = new MedianFinder();
* obj.addNum(num);
* double param_2 = obj.findMedian();
*/

参考博客:

https://blog.csdn.net/xiaosshhaa/article/details/78136032   std::ios::sync_with_stdio(false); cin.tie(0);

Leetcode 295. 数据流的中位数的更多相关文章

  1. Java实现 LeetCode 295 数据流的中位数

    295. 数据流的中位数 中位数是有序列表中间的数.如果列表长度是偶数,中位数则是中间两个数的平均值. 例如, [2,3,4] 的中位数是 3 [2,3] 的中位数是 (2 + 3) / 2 = 2. ...

  2. [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 ...

  3. LeetCode——295. Find Median from Data Stream

    一.题目链接: https://leetcode.com/problems/find-median-from-data-stream 二.题目大意: 给定一段数据流,要求求出数据流中的中位数,其中数据 ...

  4. 堆实战(动态数据流求top k大元素,动态数据流求中位数)

    动态数据集合中求top k大元素 第1大,第2大 ...第k大 k是这群体里最小的 所以要建立个小顶堆 只需要维护一个大小为k的小顶堆 即可 当来的元素(newCome)> 堆顶元素(small ...

  5. [LeetCode] 295. Find Median from Data Stream ☆☆☆☆☆(数据流中获取中位数)

    295. Find Median from Data Stream&数据流中的中位数 295. Find Median from Data Stream https://leetcode.co ...

  6. [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 ...

  7. [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 ...

  8. 295 Find Median from Data Stream 数据流的中位数

    中位数是排序后列表的中间值.如果列表的大小是偶数,则没有中间值,此时中位数是中间两个数的平均值.示例:[2,3,4] , 中位数是 3[2,3], 中位数是 (2 + 3) / 2 = 2.5设计一个 ...

  9. [Swift]LeetCode295. 数据流的中位数 | 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 ...

随机推荐

  1. 机器学习介绍(introduction)-读书笔记-

    一,什么是机器学习 第一个机器学习的定义来自于 Arthur Samuel.他定义机器学习为,在进行特定编程的情况下,给予计算机学习能力的领域.Samuel 的定义可以回溯到 50 年代,他编写了一个 ...

  2. 《Spark 官方文档》在Mesos上运行Spark

    本文转自:http://ifeve.com/spark-mesos-spark/ 在Mesos上运行Spark Spark可以在由Apache Mesos 管理的硬件集群中运行. 在Mesos集群中使 ...

  3. Web后台任务处理

    文章:.NET Core开源组件:后台任务利器之Hangfire Hangfire官网介绍:在.NET和.NET Core应用程序中执行后台处理的简便方法.无需Windows服务或单独的过程. 以持久 ...

  4. java — 垃圾回收

    1. 垃圾回收的意义 在java中,当没有对象指向原先分配给某个对象的内存的时候,这片内存就变成了垃圾,JVM的一个系统级线程就会自动释放这个内存块,垃圾回收意味着程序不再需要的对象是“无用的信息”, ...

  5. java.lang.ClassNotFoundException: com.google.gson.Gson 问题解决

    我是这么解决:把gson.jar放到WEB-INF/lib目录下. 放在其他目录就会报错.

  6. pythoh使用 xpath去除空格空格

    html_str = """ <!DOCTYPE html> <html lang="en"> <head> &l ...

  7. perf中branch-filter到底是干嘛的?

    ./arch/x86/events/intel/core.c:2161:            data.br_stack = &cpuc->lbr_stack;./arch/x86/e ...

  8. 内存交换空间(swap)的构建

    一.使用物理分区构建swap 1.先进行分区的行为. [root@iZ255cppmtxZ ~]# fdisk /dev/xvdb Welcome to fdisk (util-linux ). Ch ...

  9. java.awt.AWTError: Can't connect to X11 window server using ':20' as the value of the DISPLAY variable

    1.使用pio在Linux服务器上创建window文件时,需要使用到Linux的图形界面服务,出现以下问题需确认用户权限. 参考文献:https://zhidao.baidu.com/question ...

  10. Spring MVC架构浅析

    阅读目录 Spring MVC概述 Spring MVC框架的特点 Spring MVC工作原理 Spring MVC概述 Spring的web框架围绕DispatcherServlet设计,Disp ...