[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 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.
Example:
addNum(1)
addNum(2)
findMedian() -> 1.5
addNum(3)
findMedian() -> 2
题意:
给定一堆数据,求其中位数(将数据升序排列。若数据个数为奇,则正中为中位数;若数据个数为偶,则正中两数平均值为中位数)
思路:
显然若数据大,排序再找中位数是不现实的。
用PriorityQueue分别建maxHeap, minHeap, 并保证maxHeap.size() - minHeap.size() <=1
如此,当最终maxHeap.size() = minHeap.size() 说明数据个数为偶,取两个Heap的peek值的平均值
当最终maxHeap.size() > minHeap.size() 说明数据个数为奇,取maxHeap.peek
扫数据中的每个元素
[6, 10, 2, 6, 5, 0, 6, 3]
^ 若maxHeap.isEmpty() || 当前元素 < maxHeap.peek() , 扔到maxHeap里去

[6, 10, 2, 6, 5, 0, 6, 3]
^ 若当前元素 >= maxHeap.peek() , 扔到minHeap里去

[6, 10, 2, 6, 5, 0, 6, 3]
^ 当前元素 < maxHeap.peek() , 扔到maxHeap里去

[6, 10, 2, 6, 5, 0, 6, 3]
^ 若当前元素 >= maxHeap.peek() , 扔到minHeap里去,minHeap会自动将内部最小值sort到peek位置

[6, 10, 2, 6, 5, 0, 6, 3]
^ 当前元素 < maxHeap.peek() , 扔到maxHeap里去
此时maxHeap.size() - minHeap.size() = 2 需要维护平衡

......
如此以往,直至扫完整个数据。
代码:
class MedianFinder {
/** initialize your data structure here. */
private PriorityQueue<Integer> _maxHeap;
private PriorityQueue<Integer> _minHeap;
public MedianFinder() {
_maxHeap = new PriorityQueue<> ((o1,o2) -> o2-o1);
_minHeap = new PriorityQueue<> ((o1,o2) -> o1-o2);
}
public void addNum(int num) {
// put each integer into either maxHeap or minHeap
if(_maxHeap.isEmpty() || num < _maxHeap.peek()){
_maxHeap.add(num);
} else{
_minHeap.add(num);
}
// keep balance
if(_maxHeap.size() ==_minHeap.size()+2){
_minHeap.add(_maxHeap.poll());
}
if(_minHeap.size() ==_maxHeap.size()+1){
_maxHeap.add(_minHeap.poll());
}
}
// the number of data is even or odd
public double findMedian() {
if (_maxHeap.size() == _minHeap.size()) {
return (_maxHeap.peek() + _minHeap.peek()) / 2.0;
}else {
return _maxHeap.peek();
}
}
}
/**
* Your MedianFinder object will be instantiated and called as such:
* MedianFinder obj = new MedianFinder();
* obj.addNum(num);
* double param_2 = obj.findMedian();
*/
[leetcode]295. Find Median from Data Stream数据流的中位数的更多相关文章
- [LeetCode] 295. Find Median from Data Stream ☆☆☆☆☆(数据流中获取中位数)
295. Find Median from Data Stream&数据流中的中位数 295. Find Median from Data Stream https://leetcode.co ...
- 295 Find Median from Data Stream 数据流的中位数
中位数是排序后列表的中间值.如果列表的大小是偶数,则没有中间值,此时中位数是中间两个数的平均值.示例:[2,3,4] , 中位数是 3[2,3], 中位数是 (2 + 3) / 2 = 2.5设计一个 ...
- [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/ Median is the middle value in an ordered ...
- LeetCode——295. Find Median from Data Stream
一.题目链接: https://leetcode.com/problems/find-median-from-data-stream 二.题目大意: 给定一段数据流,要求求出数据流中的中位数,其中数据 ...
- 剑指offer 最小的k个数 、 leetcode 215. Kth Largest Element in an Array 、295. Find Median from Data Stream(剑指 数据流中位数)
注意multiset的一个bug: multiset带一个参数的erase函数原型有两种.一是传递一个元素值,如上面例子代码中,这时候删除的是集合中所有值等于输入值的元素,并且返回删除的元素个数:另外 ...
- 【LeetCode】295. Find Median from Data Stream 解题报告(C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 大根堆+小根堆 日期 题目地址:https://le ...
- 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 ...
- [LC] 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 ...
随机推荐
- css border
CSS border用于设置HTML元素(如div)的边框,包括边框的宽度.颜色和样式.本文章向码农介绍CSS border边框属性详细内容,感兴趣的码农可以参考一下. CSS 边框即CSS bord ...
- IIS7根据PID查找对应的站点
runas /user:administrator cmd cd \Windows\System32\inetsrv appcmd.exe list wp
- python-django-ORM,常用查询方式
介绍django model 的一些常用查询方式 首先是一些文档性的帮助 __exact 精确等于 like ‘aaa’ __iexact 精确等于 忽略大小写 ilike ‘aaa’ __conta ...
- 不规则ROI的提取
在网上看到基于opencv3.0之前的API实现不规则ROI的提取,我自己试了一下发现opencv3.0不行,第一想法是我写的有问题,最后发现是API的改版.原理很简单. 目标:提取黑线作为ROI 原 ...
- LaunchFaster 启动器工具 - 类似 Rolan 和音速启动的图标式快捷启动软件
LaunchFaster 启动器是本人近期编写的一款windows平台上快速启动应用的开源工具软件. LaunchFaster 启动器是一款类似于 Rolan 和 音速启动 和 Lily 的图标形式的 ...
- UnicodeEncodeError: 'gbk' codec can't encode character '\xbb' in position
python实现爬虫遇到编码问题: error:UnicodeEncodeError: 'gbk' codec can't encode character '\xXX' in position XX ...
- CUDA C Programming Guide 在线教程学习笔记 Part 5
附录 A,CUDA计算设备 附录 B,C语言扩展 ▶ 函数的标识符 ● __device__,__global__ 和 __host__ ● 宏 __CUDA_ARCH__ 可用于区分代码的运行位置. ...
- HTML5 Canvas ( 文字的度量 ) measureText
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...
- java ee7 软件安装和环境配置
1. java ee sdk 最新版下载地址 Java EE软件开发包(Software Development Kit, SDK) http://www.oracle.com/technetwork ...
- Flash Builder编译的swf为什么在bin-debug下运行正常,复制到其他文件夹就不正常
在Flash Builder/Flex Builder中,可以使用编译参数-use-network=false实现