当前位置:网站首页>从数据流中快速查找中位数
从数据流中快速查找中位数
2022-07-30 09:50:00 【Currybeefer】
这题的思路就是维护两个堆,一个大根堆一个小根堆,将数据加入到这两个堆中,这样从两个堆的堆顶就可以获取整个数据的中位数。
C++中的priority_queue 默认是以大根堆存储的,也就是说,堆顶是堆中最大的元素。如果要定义小根堆,那么就得自己写一个仿函数。具体看代码注释
class MedianFinder
{
class cmp
{
public:
bool operator()(double a,double b)
{
return a>b;//返回true时,优先级会更低,也就是a的优先级会低于b,a会往堆底调整
}
};
priority_queue<double,vector<double>,cmp> Small;//构建小顶堆
priority_queue<double,vector<double>> Large;//构建大顶堆
public:
MedianFinder()
{
}
void addNum(int num)
{
if(Large.size()==0 && Small.size()==0)//第一个加入的元素默认先放在Large
{
Large.push(num);
return;
}
if(num<=Large.top())//如果num小于Large的堆顶,那么num就放入Large堆中
{
Large.push(num);
cout<< num<<"push to large"<<endl;
}
else//若num大于Large堆顶,也就是比Large最大的大,那么放入Small中
{
Small.push(num);
cout<< num<<"push to small"<<endl;
}
//如果两个堆的数量之差为2,那么需要重新调整一下大小,把大的那个堆顶元素放在小的那个堆上
//为什么差为2的时候调整?因为这样就可以保证调整完后,两个堆数量一致。(3:1)->(2:2)这样的
if(Large.size()-Small.size()==2)
{
int temp=Large.top();
Large.pop();
Small.push(temp);
return;
}
if(Small.size()-Large.size()==2)
{
int temp=Small.top();
Small.pop();
Large.push(temp);
return;
}
}
double findMedian()
{
if(Small.size()>Large.size())
return Small.top();
else if(Small.size()<Large.size())
return Large.top();
return (Small.top()+Large.top())/2;
}
};
边栏推荐
- hcip06 ospf special area comprehensive experiment
- Four ways the Metaverse is changing the way humans work
- 一个近乎完美的 Unity 全平台热更方案
- Meikle Studio-Look at Hongmeng Device Development Practical Notes 7-Network Application Development
- GNOME 新功能:安全启动被禁用时警告用户
- ospf2 two-point two-way republish (question 2)
- Pytorch中 nn.Transformer的使用详解与Transformer的黑盒讲解
- Day113. Shangyitong: WeChat login QR code, login callback interface
- Quick Start Tutorial for flyway
- 第1章 Kali与靶机系统
猜你喜欢
Re17:读论文 Challenges for Information Extraction from Dialogue in Criminal Law
By building a sequence table - teach you to calculate time complexity and space complexity (including recursion)
Nacos configuration in the project of battle
唯物辩证法-条件论
Do you really understand the 5 basic data structures of Redis?
EViews 12.0软件安装包下载及安装教程
JVM内存布局、类加载机制及垃圾回收机制详解
死锁的理解
论文阅读:SegFormer: Simple and Efficient Design for Semantic Segmentation with Transformers
你真的懂Redis的5种基本数据结构吗?
随机推荐
数据库脏读、不可重复读、幻读以及对应的隔离级别
Quick Start Tutorial for flyway
再有人问你分布式事务,把这篇扔给他
这种叫什么手法
OC-ARC (Automatic Reference Counting) automatic reference counting
第1章 Kali与靶机系统
(Text) Frameless button settings
Nacos configuration in the project of battle
Re15:读论文 LEVEN: A Large-Scale Chinese Legal Event Detection Dataset
hcip06 ospf special area comprehensive experiment
分页 paging
The method of parameter passing
新一代开源免费的终端工具,太酷了
Re18:读论文 GCI Everything Has a Cause: Leveraging Causal Inference in Legal Text Analysis
OC - Manual Reference Counting Memory Management
Re17: Read the paper Challenges for Information Extraction from Dialogue in Criminal Law
Re21: Read the paper MSJudge Legal Judgment Prediction with Multi-Stage Case Representation Learning in the Real
元宇宙改变人类工作模式的四种方式
Beijing suddenly announced big news in the Metaverse
PyQt5 - draw text on window