[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 ...
随机推荐
- js判断各种浏览器
<script type="text/javascript"> if (navigator.appName.indexOf("Microsoft Intern ...
- Fork/Join编程模型
1.一种并行计算的多线程编程模型 2.开始--任务分割--多线程异步执行---任务合并--阻塞等待合并结果.(分治算法) 3.work-stealing算法: 每个线程维护一个各自的双端的链表,有新任 ...
- tcp 大文件上传 ,切换目录 及登陆文件加盐处理
实现大文件的传输 服务器 import socketimport jsonimport structsk = socket.socket()sk.bind(("127.0.0.1" ...
- Executor框架(六)CompletionService 接口
CompletionService 接口是一个独立的接口,并没有扩展 ExecutorService . 其默认实现类是ExecutorCompletionService; 接口 Comple ...
- linux7安装teamViewer
参考网站:http://blog.sina.com.cn/s/blog_15308c8290102x72u.html 下载网站:https://www.teamviewer.com/zhCN/down ...
- HTML5 Canvas ( 贝塞尔曲线, 一片星空加绿地 ) quadraticCurveTo, bezierCurveTo
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...
- 2.mybatis实战教程(mybatis in action)之二:以接口的方式编程
转自:http://www.yihaomen.com/article/java/304.htm 前面一章,已经搭建好了eclipse,mybatis,mysql的环境,并且实现了一个简单的查询. 请注 ...
- discuz 修改积分策略( 在周期中添加"每周" )
在 source/admincp/admincp_credits.php 文件中, ctrl+f 搜索 $lang['setting_credits_policy_cycletype_1'] 处, ...
- 可视化库-seaborn-单变量绘图(第五天)
1. sns.distplot 画直方图 import numpy as np import pandas as pd from scipy import stats, integrate impor ...
- overflow: auto 图片自适应调整
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...