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数据流的中位数的更多相关文章

  1. [LeetCode] 295. Find Median from Data Stream ☆☆☆☆☆(数据流中获取中位数)

    295. Find Median from Data Stream&数据流中的中位数 295. Find Median from Data Stream https://leetcode.co ...

  2. 295 Find Median from Data Stream 数据流的中位数

    中位数是排序后列表的中间值.如果列表的大小是偶数,则没有中间值,此时中位数是中间两个数的平均值.示例:[2,3,4] , 中位数是 3[2,3], 中位数是 (2 + 3) / 2 = 2.5设计一个 ...

  3. [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 ...

  4. leetcode@ [295]Find Median from Data Stream

    https://leetcode.com/problems/find-median-from-data-stream/ Median is the middle value in an ordered ...

  5. LeetCode——295. Find Median from Data Stream

    一.题目链接: https://leetcode.com/problems/find-median-from-data-stream 二.题目大意: 给定一段数据流,要求求出数据流中的中位数,其中数据 ...

  6. 剑指offer 最小的k个数 、 leetcode 215. Kth Largest Element in an Array 、295. Find Median from Data Stream(剑指 数据流中位数)

    注意multiset的一个bug: multiset带一个参数的erase函数原型有两种.一是传递一个元素值,如上面例子代码中,这时候删除的是集合中所有值等于输入值的元素,并且返回删除的元素个数:另外 ...

  7. 【LeetCode】295. Find Median from Data Stream 解题报告(C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 大根堆+小根堆 日期 题目地址:https://le ...

  8. 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 ...

  9. [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 ...

随机推荐

  1. http://www.cnblogs.com/TankXiao/archive/2012/02/06/2337728.html

    http://www.cnblogs.com/TankXiao/archive/2012/02/06/2337728.html

  2. HADOOP与HDFS数据压缩格式

    1.cloudera 数据压缩的一般准则 一般准则 是否压缩数据以及使用何种压缩格式对性能具有重要的影响.在数据压缩上,需要考虑的最重要的两个方面是 MapReduce 作业和存储在 HBase 中的 ...

  3. 常用模块:hashlib,subprocess,configparser。

    一  hashlib模块 那么我们前面学习数据类型的时候,也讲了hash,可变类型不可hash:不可变类型可hash. 我们知道hash是一种算法,接收传入的内容经过运算之后得到一个hash值,我们可 ...

  4. Java技术栈

    内容: 1.Java基础(JavaSE) 2.数据结构与算法与设计模式 3.计算机理论知识 4.数据库 5.Java web(JavaEE) 6.消息队列 7.Linux及服务器相关 8.分布式相关 ...

  5. django-查询按时间排序

    Meta类实现 class News(models.Model): title = models.CharField(max_length=35) desc = models.CharField(ma ...

  6. 温故而知新复习下PHP面向对象

    面向对象在PHP中是以类的形势展示的 PHP中的类是单继承的,用关键字extends来实现继承父类, 关键字public protected private 第一个是公开的 谁都可以访问,第二个只能本 ...

  7. win10 + Ubuntu 双系统,重装后的引导修复,时间调整和启动项调整

    ▶ 原先为 win10 + Ubuntu 双系统,使用 grub2 作引导,在重装了 win10 (大作死升到了1803)后系统重写了引导,启动项里找不到 Ubuntu,需要修复. ● 参考[http ...

  8. apo 简单参考

    参考: https://www.cnblogs.com/Geyoung/p/6927905.html @Aspect @Component public class TimeAspect { //通过 ...

  9. JS检测当前设备是PC还是移动端

    用到的时候找到的分享下,也是收藏下,本地收藏多了感觉找起来很麻烦 方法1: function IsPC() { var userAgentInfo = navigator.userAgent; var ...

  10. sql 数据库修复

    数据库修复 exec sp_dboption 'dbname1','single user',‘true’ dbcc checkdb('dbname1') dbcc checkdb('dbname1' ...