当前位置:网站首页>从数据流中快速查找中位数
从数据流中快速查找中位数
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;
}
};
边栏推荐
- 学习笔记10--局部轨迹生成主要方法
- (BUG record) No module named PIL
- [100 Solidity Skills] 1. Contract reentrancy attack
- PyQt5 - draw text on window
- PyQt5-在窗口上绘制文本
- Soft test system architects introductory tutorial | system operation and software maintenance
- 606. 根据二叉树创建字符串(视频讲解!!!)
- 多线程--线程和线程池的用法
- WARN: Establishing SSL connection without server's identity verification is not recommended when connecting to mysql
- In the robot industry professionals, Mr Robot industry current situation?
猜你喜欢

Re17: Read the paper Challenges for Information Extraction from Dialogue in Criminal Law

WARN: Establishing SSL connection without server's identity verification is not recommended when connecting to mysql

Do you really understand the 5 basic data structures of Redis?

Re18:读论文 GCI Everything Has a Cause: Leveraging Causal Inference in Legal Text Analysis

(文字)无框按钮设置

Adaptive Control - Simulation Experiment 1 Designing Adaptive Laws Using Lyapunov's Stability Theory

死锁的理解

梅科尔工作室-看鸿蒙设备开发实战笔记七——网络应用开发

A new generation of free open source terminal tool, so cool

Re15:读论文 LEVEN: A Large-Scale Chinese Legal Event Detection Dataset
随机推荐
OC - Manual Reference Counting Memory Management
Flask's routing (app.route) detailed
[100个Solidity使用技巧]1、合约重入攻击
Nacos configuration in the project of battle
PyQt5 - draw sine curve with pixels
hcip06 ospf特殊区域综合实验
Re20:读论文 What About the Precedent: An Information-Theoretic Analysis of Common Law
百度推广助手遇到重复关键字,验证错误,怎么一键删除多余的
Matplotlib--plot markers
OC-手动引用计数内存管理
第3章 信息收集
Soft Exam System Architect Concise Tutorial | Case Analysis | Requirement Analysis
idea2021+Activiti【最完整笔记一(基础使用)】
Security思想项目总结
神秘的APT攻击
一个近乎完美的 Unity 全平台热更方案
阿里云OSS对象存储
CVTE school recruitment written test questions + summary of knowledge points
606. Create a string from a binary tree (video explanation!!!)
【HMS core】【Analytics Kit】【FAQ】如何解决华为分析付费分析中付款金额显示为0的问题?