[Leetcode] 295. Find Median from Data Stream 解题报告

C# Socket通信从入门到精通(14)——多个异步UDP客户端C#代码实现(史上最全) 本文详细介绍了Socket通信中多个异步Udp客户端功能的实现、介绍如何使用多个客户端与多个服务器进行数据的交互,包含连接服务器、断开服务器、发送数据、读取数据、C#代码实现,由高级工程师多年经验总结而成,绝对干货! 阅读详情

题目:

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:

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

思路:

最优解是维护一个大顶堆和一个小顶堆,并且保证大顶堆的大小不小于小顶堆的大小,其中大顶堆里面包含数组中小的一半元素,小顶堆里面包含数组中大的一半元素。这样大顶堆的首元素是小一半里面最大的元素,而小顶堆的首元素是大一半里面最小的元素。如果大顶堆和小顶堆的大小相同,则中位数就是两者首元素的平均值;否则就是大顶堆的首元素。而添加一个元素的方法是:首先判断这个元素应该出现在大顶堆还是小顶堆,并且将其插入相应的堆中。之后还要根据情况调整两个堆的大小,保证大顶堆比小顶堆多一个元素或者两者元素数相同。一个巧妙的方法是:首先将该元素插入大顶堆中,然后将大顶堆的首元素移到小顶堆中;移动之后如果发现大顶堆的大小小于小顶堆,则将小顶堆的首元素移到大顶堆中。

在C++的实现中,既可以用数组来模拟heap实现,也可以用priority_queue来实现,还可以用multiset来实现,非常方便。

代码:

class MedianFinder {
public:
    /** initialize your data structure here. */
    MedianFinder() {
        
    }
    
    void addNum(int num) {      // the time complexity is O(logn)
        small.push(num);  
        large.push(-small.top());  
        small.pop();  
        if(small.size() < large.size()) {  
            small.push(-large.top());  
            large.pop();  
        }  
    }
    
    double findMedian() {       // the time complexity is O(1)
        int size = small.size() + large.size();
        if (size % 2 != 0) {
            return small.top();
        }
        else {
            return (small.top() - large.top()) / 2.0;
        }
    }
private:
    priority_queue<int> small, large;
};

/**
 * Your MedianFinder object will be instantiated and called as such:
 * MedianFinder obj = new MedianFinder();
 * obj.addNum(num);
 * double param_2 = obj.findMedian();
 */
杰理AC695N芯片跑iic接口的oled驱动分享 本文介绍了一个基于IIC接口的0.96寸OLED显示屏驱动程序,适用于SSD1306控制器。驱动采用软件模拟IIC方式,支持字符、数字、字符串、汉字和图片显示,测试平台为杰理AC695N系列芯片。硬件参数包括128×64分辨率,IIC接口(地址0x78),使用IO_PORTA_02(SDA)和IO_PORTA_03(SCL)引脚。驱动包含OLED主文件(oled.c/h)、软件IIC实现(soft_iic.c/h)和字体/图片数据文件(oledfont.h/bmp.h),可通过修改soft_iic.h中的引 阅读详情

相关推荐

NDVI空间分布变化Slope趋势分析与F检验(自用)

1.NDVI数据预处理2.一元线性回归分析3.Slope趋势分析与F检验

什么都学亿点点的博客 9022

MDK开发中 __packed 与 #pragma packed 的指针安全性与编译器行为深度解析

本文深入解析了MDK开发中`__packed`与`#pragma packed`的指针安全性与编译器行为差异。通过实际案例和代码示例,详细探讨了字节对齐在嵌入式开发中的重要性,以及两种对齐方式在AC5与AC6编译器下的不同表现。文章还提供了最佳实践和调试技巧,帮助开发者避免常见陷阱,确保代码的稳定性和可移植性。

weixin_30576827的博客 505

Lucene学习总结之二:Lucene的总体架构

Lucene总的来说是: 一个高效的,可扩展的,全文检索库。 全部用Java实现,无须配置。 仅支持纯文本文件的索引(Indexing)和搜索(Search)。 不负责由其他格式的文件抽取纯文本文件,或从网络中抓取文件的过程。 在Lucene in action中,Lucene 的构架和过程如下图, 说明Lucene是有索引和搜索的两个过程,包

forfuture1978的专栏 1万+

295. Find Median from Data Stream

题目描述 如何求数据流的中位数情况,这个数据流可以随时添加数据,此题要找出数据流的中位数,数据流由无序整数组成,并且数据流是在变化的。 数据流顺序是无序的,添加的顺序也是无序,但是求解是中位数的话就要保持有序的状态,所以基本 Median is the middle value in an ordered integer list. If the size of the list is even,...

qq_28350997的博客 236

统信UOS V20 安装mysql5.7.42详细教程

到bin/下,执行下面的命令,初始化mysql服务信息,得到数据库的初始密码,实际如果目录不一样记得换成自己的目录。下载后通过远程将安装包上传至服务器,我这里将安装包上传至/usr/local/mysql 下。ps:这时候如果还是不能远程访问的话,可能是防火墙的原因,命令行执行以下命令即可。再次登录,发现缺少 libtinfo.so.5文件,再次建立软连接。登录,发现报错,提示缺少libncurses.so.5文件,使用。创建 /var/lib/ 下mysql目录。登录成功,修改密码,设置远程访问。

为谁伫立在风中 1万+

TMS320F28388D 学习笔记2 高精度PWM (HRPWM)

HRPWM的学习记录。

whyorwhnt的专栏 9547

LeetCode 295. Find Median from Data Stream(优先队列)

295. Find Median from Data Stream Hard 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 val...

da_kao_la的博客 330

Matlab并行运算

今天搞了一下matlab的并行计算,效果好的出乎我的意料。 本来CPU就是双核,不过以前一直注重算法,没注意并行计算的问题。今天为了在8核的dell服务器上跑程序才专门看了一下。本身写的程序就很容易实现并行化,因为beamline之间并没有考虑相互作用。等于可以拆成n个线程并行,要是有550核的话,估计1ms就算完了。。。 先转下网上找到的资料。 一、Matlab并行计算原理梗概 Matl

wenyusuran的专栏 1万+

[leetcode] 295. Find Median from Data Stream 解题报告

题目链接: 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 i

小榕流光的专栏 3591

6.4 实战项目:基于Agent内核构建Code Review智能体

本文介绍了一个基于Agent内核构建的智能Code Review系统实战项目。系统采用多Agent协作架构,包含代码质量分析、安全漏洞识别、性能优化建议等核心功能模块。文章详细展示了系统架构设计,采用协调器Agent管理多个专业Agent协同工作,并提供了基础Agent框架和协调器Agent的TypeScript实现代码。项目结合了AI Agent核心原理和多Agent协作机制,实现了自动化的代码审查流程,能够生成详细的审查报告,帮助开发者提升代码质量和安全性。

yonggeit的博客 3121

Codex 调试记录获取完全指南:日志查看、工具调用与实战排查教程

本文主要介绍了Codex 调试记录获取完全指南:日志查看、工具调用与实战排查教程,希望能对使用Codex的同学们有所帮助。 文章目录 1. 背景 2. 实战教程

weixin_43178406的博客 7万+

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

负雪明烛 1532

pscad IEEE标准系统搭建说明

pscad IEEE标准系统搭建说明,包含典型的IEEE 9节点、14节点、30节点、39节点和118节点系统。内含标准测试系统的原始数据和潮流结果。

LeetCode 295. Find Median from Data Stream 题解

本题是双堆技巧的经典应用。使用两个堆维护数据的两半保持堆的大小平衡中位数在堆顶。

cannonjinx的博客 4332

Probability and Statistics - 4th edition

Probability and Statistics - 4th edition

IMSL CNL 7.0.0 (x86-64)Crack

IMSL CNL 7.0.0 (x86-64)Crack intel的算法库破解文件,这是C++版本的破解文件

[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 middle value. So the median is the mean of the two middle value. Examples:  [2,3,4] , the median

coder 进阶的专栏 2767

数学建模模型总结

这是本人在两年内准备参加数学建模竞赛搜集到的模型资料,已经经过了本人的筛选和整理,希望能够对准备参加数学建模竞赛的学弟学妹们提供一些帮助!

Leetcode295 Find Median from Data Stream解题方法

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 middle value. So the median is the mean of the two middle value. For example, [2,3,4]`, the median is `3 [2,3

初七的博客 261

LeetCode295——Find Median from Data Stream

LeetCode295——Find Median from Data Stream 前提是Ordered List。 但是插入即addNum操作可能是无序的。 尝试过对序列排序再返回的办法结果是超时的。想想还是天真,代码: class MedianFinder { private: vectornums; public: // Adds a number into the data

上善若水 1387

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 middle value. So the median is the mean of the two middle value. Examples:  [2,3,4] , the median

无名山丘,崛起成峰 1414

Leetcode 295. Find Median from Data Stream (python+cpp)

Leetcode 295. Find Median from Data Stream题目解法1:simple sorting解法2:插入解法3:two heaps 题目 解法1:simple sorting 每次找中位数的操作都将列表进行重新排序,这种解法时间复杂度为O(nlogn) class MedianFinder: def __init__(self): """ initialize your data structure here. ""

qq_37821701的博客 692

[Leetcode]295. Find Median from Data Stream

Leetcode 295. Find Median from Data Stream

正西风落叶下长安 958

LeetCode:295. Find Median from Data Stream

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 middle value. So the median is the mean of the two m...

杨领well的专栏 233

LeetCode 295. Find Median from Data Stream【堆】困难

The median is the middle value in an ordered integer list. If the size of the list is even, there is no middle value and the median is the mean of the two middle values. For example, for arr = [2,3,4], the median is 3. For example, for arr = [2,3], the

memcpy0的博客 593

[leetcode] 295. Find Median from Data Stream @ python

原题 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...

闲庭信步 933

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 middle value. So the median is the mean of the two middle value. For example, [2,3,4], the median ...

taoqick的专栏 372

LeetCode 295.Find Median from Data Stream【Java】

题目描述 295. 数据流的中位数 AC代码 /* 解题思路:可以通过对顶堆(大根堆和小根堆)来解决。 把一组数(假设N个数)分成两拨,大根堆和小根堆维护的个数尽量不超过1个(N/2),小根堆可以比大根堆多一个元素。 小根堆维护较大的N/2个数字,大根堆维护较小的N/2个数字,那么小根堆的top()和大根堆的top()就是这N个数中间的两个数了。 插入的时候: 1.如果大根堆是空的,或者要插入的元...

NayelyA的博客 227
上一篇: [Leetcode] 294. Flip Game II 解题报告
下一篇: [Leetcode] 296. Best Meeting Point 解题报告
魔豆Magicbean
博客等级 码龄19年 146粉丝 742原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值