当前位置:网站首页>【剑指offer】面试题41:数据流中的中位数——大、小堆实现
【剑指offer】面试题41:数据流中的中位数——大、小堆实现
2022-07-27 14:24:00 【Jocelin47】

方法一:排序取中位数(超时)
class MedianFinder {
public:
/** initialize your data structure here. */
vector<int> record;
MedianFinder() {
}
void addNum(int num) {
record.push_back(num);
}
double findMedian() {
double result = 0;
if( record.size() == 0 )
return result;
sort( record.begin(), record.end() );
//奇数
if( record.size() & 1 )
{
result = record[ record.size() / 2 ];
}
else
{
result = (double)( record[ record.size() / 2 - 1 ] + record[ record.size() / 2 ] ) / 2;
}
return result;
}
};
/** * Your MedianFinder object will be instantiated and called as such: * MedianFinder* obj = new MedianFinder(); * obj->addNum(num); * double param_2 = obj->findMedian(); */
方法二:大、小堆实现
1.通过两个堆,一个最大堆一个最小堆,大小不超过1 ,如果超过1就把大小堆平衡一下
2.中位数如果是奇数则为最大堆的top或最小堆的top,如果最大堆和最小堆的大小相等,则取两个堆顶的平均值
3.最小堆里面的数据都要比最大堆大
class MedianFinder {
public:
/** initialize your data structure here. */
//最大堆
priority_queue< int, vector<int> > max_heap;
//最小堆
priority_queue< int, vector<int>, greater<int> > min_heap;
MedianFinder() {
}
void addNum(int num) {
if( max_heap.empty() || num < max_heap.top() )
{
max_heap.push( num );
}
else
min_heap.push( num );
//维持堆得大小
if( max_heap.size() == min_heap.size() + 2)
{
min_heap.push(max_heap.top());
max_heap.pop();
}
else if( min_heap.size() == max_heap.size() + 2)
{
max_heap.push( min_heap.top() );
min_heap.pop();
}
}
double findMedian() {
if( ( max_heap.size() + min_heap.size() ) == 0 )
return -1;
if( max_heap.size() > min_heap.size() )
{
return max_heap.top();
}
else if( max_heap.size() < min_heap.size() )
{
return min_heap.top();
}
else
return double( max_heap.top() + min_heap.top() ) / 2;
}
};
边栏推荐
- Network equipment hard core technology insider router Chapter 14 from deer by device to router (middle)
- Spark 3.0 测试与使用
- Simple mathematical knowledge related to 3D
- Leetcode 1143. dynamic programming of the longest common subsequence /medium
- 3D相关的简单数学知识
- Leetcode-1737- minimum number of characters to change if one of the three conditions is met
- MLX90640 红外热成像仪测温传感器模块开发笔记(七)
- 【剑指offer】面试题53-Ⅱ:0~n-1中缺失的数字——二分查找
- Unity性能优化------DrawCall
- AssetBundle如何打包
猜你喜欢

Singles cup, web:web check in

Leetcode interview question 17.21. water volume double pointer of histogram, monotonic stack /hard

EMC design scheme of USB2.0 Interface

STM32 can communication filter setting problem

How to edit a framework resource file separately

HaoChen CAD building 2022 software installation package download and installation tutorial

flutter —— 布局原理与约束

光电隔离电路设计方案(六款基于光耦、AD210AN的光电隔离电路图)

华为鸿蒙模拟器去除顶部导航栏方法

3.3-5v转换
随机推荐
【剑指offer】面试题56-Ⅰ:数组中数字出现的次数Ⅰ
Principle of MOS tube to prevent reverse connection of power supply
西瓜书《机器学习》阅读笔记之第一章绪论
Leetcode interview question 17.21. water volume double pointer of histogram, monotonic stack /hard
Leetcode 244周赛-赛后补题题解【西兰花选手】
Spark 任务Task调度异常分析
Leetcode 240. search two-dimensional matrix II medium
Selenium 报错:session not created: This version of ChromeDriver only supports Chrome version 81
3.3-5v conversion
Unity performance optimization ----- occlusion culling of rendering optimization (GPU)
Basic usage of kotlin
“router-link”各种属性解释
【剑指offer】面试题55 - Ⅰ/Ⅱ:二叉树的深度/平衡二叉树
使用解构交换两个变量的值
Design scheme of digital oscilloscope based on stm32
Leetcode 456.132 mode monotone stack /medium
USB interface electromagnetic compatibility (EMC) solution
shell脚本读取文本中的redis命令批量插入redis
谷粒商城配置CorsWebFilter后,报错:Resource sharing error:MultipleAllowOriginValues
Leetcode 783. binary search tree node minimum distance tree /easy