LeetCode OJ: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 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:
add(1)
add(2)
findMedian() -> 1.5
add(3)
findMedian() -> 2
如上,可以维护一个最大堆以及一个最小堆,如果小堆数的数量大于大堆数量。那么可以把小堆中的最小值返还给大堆。(注意这个值相当于大堆中的最大者,因为小堆中的值都是大堆中转移过去的)。代码如下:
class MedianFinder {
public:
// Adds a number into the data structure.
void addNum(int num) {
maxHeap.push(num);
int tmp = maxHeap.top();
maxHeap.pop();
minHeap.push(tmp);
if(minHeap.size() > maxHeap.size()){
tmp = minHeap.top();
minHeap.pop();
maxHeap.push(tmp);
}
}
// Returns the median of current data stream
double findMedian() {
int m = maxHeap.size();
int n = minHeap.size();
if(m > n)
return maxHeap.top()/1.0;
else
return (maxHeap.top() + minHeap.top())/2.0;
}
private:
priority_queue<int, vector<int>, greater<int>> maxHeap;
priority_queue<int, vector<int>, less<int>> minHeap;
};
LeetCode OJ: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 ...
- [LeetCode] 295. Find Median from Data Stream ☆☆☆☆☆(数据流中获取中位数)
295. Find Median from Data Stream&数据流中的中位数 295. Find Median from Data Stream https://leetcode.co ...
- [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 ...
- leetcode@ [295]Find Median from Data Stream
https://leetcode.com/problems/find-median-from-data-stream/ Median is the middle value in an ordered ...
- [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 ...
- LeetCode——295. Find Median from Data Stream
一.题目链接: https://leetcode.com/problems/find-median-from-data-stream 二.题目大意: 给定一段数据流,要求求出数据流中的中位数,其中数据 ...
- [LeetCode] 346. Moving Average from Data Stream 从数据流中移动平均值
Given a stream of integers and a window size, calculate the moving average of all integers in the sl ...
- 剑指offer 最小的k个数 、 leetcode 215. Kth Largest Element in an Array 、295. Find Median from Data Stream(剑指 数据流中位数)
注意multiset的一个bug: multiset带一个参数的erase函数原型有两种.一是传递一个元素值,如上面例子代码中,这时候删除的是集合中所有值等于输入值的元素,并且返回删除的元素个数:另外 ...
- [LeetCode] Find Median from Data Stream
Find Median from Data Stream Median is the middle value in an ordered integer list. If the size of t ...
随机推荐
- Linux下编程学习一
本篇主要记录一些在学习LINUX下编程时,, C和C++语言的一些基础的常识, 一. 函数指针 void MyFun(int x); 函数声明 void (*FunP)(int ); 函数指针声明 下 ...
- tomcat进程意外退出的问题分析(转)
原文链接:http://hongjiang.info/why-kill-2-cannot-stop-tomcat/ 节前某个部门的测试环境反馈tomcat会意外退出,我们到实际环境排查后发现不是jvm ...
- Oracle中验证非空的函数NVL(),NVL2()总结
1.NVL()函数 NVL函数的格式如下: NVL(expr1,expr2) 含义是:如果oracle第一个参数为空那么显示第二个参数的值,如果第一个参数的值不为空,则显示第一个参数本来的值. 2 N ...
- MySQL多版本并发控制机制(MVCC)-源码浅析
MySQL多版本并发控制机制(MVCC)-源码浅析 前言 作为一个数据库爱好者,自己动手写过简单的SQL解析器以及存储引擎,但感觉还是不够过瘾.<<事务处理-概念与技术>>诚然 ...
- Nginx配置location跳转后偶尔出现404
tv.xxx.com/voice请求时需跳转至:tv.xxx.com/zongyi/zt2015/haoshengyin/index.shtml 目录结构: nginx服务器配置: location ...
- dependency的scope
scope作用域,一共有三种作用域:compile(编译).runtime(运行).test(测试) 备注:打包必经compile阶段 1.test 测试 2.compile 编译 打 ...
- c# c++通信--命名管道通信
进程间通信有很多种,windows上面比较简单的有管道通信(匿名管道及命名管道) 最近做个本机c#界面与c++服务进行通信的一个需求.简单用命名管道通信.msdn都直接有demo,详见下方参考. c+ ...
- 20145303刘俊谦 Java 代码托管
(20145303刘俊谦) Java 第三周代码托管 这是最近保存下来的代码,今天一起上传的,有很多在代码学习过程中无意识删掉了:
- 解读:MR多路径输入
对于在一个MR-Job中使用多路径作为输入文件,一般有三种方法: 1).多次调用,加载不同路径: import org.apache.hadoop.mapreduce.lib.input.FileIn ...
- Spring boot 解决 hibernate no session异常
启动类中加入 @Beanpublic OpenEntityManagerInViewFilter openEntityManagerInViewFilter(){ return new OpenEnt ...