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. Android SDK的安装与环境配置

    一.Android SDK工具下载.安装 Android SDK工具下载:http://www.androiddevtools.cn/ SDK下载页面如下,由于电脑Windows系统所以下载的Wind ...

  2. vb 导出excel生成图表统计

    Private Sub btnExprot_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnE ...

  3. 柒月风华BBS上线

    论坛地址:https://3003soft.top/LBBS/ 欢迎大家加入. 开放式轻论坛:记录好玩.有趣的事儿:一起努力,一起前进: 希望能建立一个分享各类解决方案的社区

  4. OpenCL 双调排序 GPU 版

    ▶ 参考书中的代码,写了 ● 代码,核函数文件包含三中算法 // kernel.cl __kernel void bitonicSort01(__global uint *data, const ui ...

  5. aix系统使用随笔

    在 Aix操作系统 中,常用的文档编辑命令是 vi.下面,我们就来学习一下有关vi的使用决窍. 在vi中,必须牢记它是有两个状态的 ---- 输入状态与命令状态.由输入状态切换 到命令状态,必须ESC ...

  6. JAVA Spring JavaBean 属性值的注入方式( 属性注入, 特殊字符注入 <![CDATA[ 带有特殊字符的值 ]]> , 构造器注入 )

    <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.sp ...

  7. leetcode401

    public class Solution { /// <summary> /// 将二进制字符串转为十进制数字 /// </summary> /// <param na ...

  8. leetcode237

    /** * Definition for singly-linked list. * public class ListNode { * public int val; * public ListNo ...

  9. eclipse启动tomcat无法访问的解决方法

    转自:https://www.cnblogs.com/longshiyVip/p/4637680.html 问题:: tomcat在eclipse里面能正常启动,但在浏览器中访问http://loca ...

  10. socket 网络连接基础

    socket 客户端 import socket 1.client = socket.socket()  # socket.TCP/IP 选择连接的类型,默认为本地连接 2.client.connec ...