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

295. Find Median from Data Stream

https://leetcode.com/problems/find-median-from-data-stream/discuss/74062/Short-simple-JavaC%2B%2BPython-O(log-n)-%2B-O(1)

题目

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

题目描述

如何得到一个数据流中的中位数?如果从数据流中读出奇数个数值,那么中位数就是所有数值排序之后位于中间的数值。如果从数据流中读出偶数个数值,那么中位数就是所有数值排序之后中间两个数的平均值。

解析

如上图所示,如果数据在容器中已经排序,那么中位数可以由P1和P2指向的数得到。如果容器中数据的个数是奇数,那么P1和P2指向同一个数据。
  注意到,整个容器被分隔成了两部分。位于容器左边部分的数据比右边的数据小。另外 P1指向的是左边的最大数据, P2指向的是右边部分最小的数。
  如果能够保证数据容器左边的数据都小于右边的数据,这样即使左右两边内部的数据没有排序,也可以根据左边最大的数和右边最小的数得到中位数。用最大推可以快速的从一个数据容器中找出最大数,最小堆可以快速的从一个数据容器中找出最小的数。
  用一个最大堆实现左边的数据容器,用最小堆实现右边的数据容器。
  首先,要保证数据平均分配到两个堆中。为了实现平均分配,可以在数据的总数目是偶数时把新数据插入到最小堆,否则插入到最大堆中。
  还要保证最大堆中的数据都要小于最小堆中的数据。如果当前数据的总数目是偶数,也就是要插入最小堆,但是它比最大堆中的一些数还要小。此时,先将这个数插入到最大堆中,然后把最大堆中最大的数取出,插入到最小堆中。如果当前数据的总数目是奇数,也就是要插入最大堆,但是它比最小堆中的一些数还要大。此时,先将这个数插入到最小堆中,然后把最小堆中最小的数取出,插入到最大堆中。

代码实现

Time Complexity: addNum - O(logn)  , findMedian - O(1),  Space Complexity - O(n)

class MedianFinder {
private PriorityQueue<Integer> maxOrientedHeap;
private PriorityQueue<Integer> minOrientedHeap; public MedianFinder() {
this.minOrientedHeap = new PriorityQueue<Integer>();
this.maxOrientedHeap = new PriorityQueue<Integer>(10, new Comparator<Integer>() {
public int compare(Integer i1, Integer i2) {
return i2 - i1;
}
});
}
// Adds a number into the data structure.
public void addNum(int num) {
maxOrientedHeap.add(num); // O(logn)
minOrientedHeap.add(maxOrientedHeap.poll()); // O(logn)
if(maxOrientedHeap.size() < minOrientedHeap.size()) {
maxOrientedHeap.add(minOrientedHeap.poll()); //O(logn)
}
} // Returns the median of current data stream
public double findMedian() { // O(1)
if(maxOrientedHeap.size() == minOrientedHeap.size())
return (maxOrientedHeap.peek() + minOrientedHeap.peek()) / 2.0;
else
return maxOrientedHeap.peek();
}
}; // Your MedianFinder object will be instantiated and called as such:
// MedianFinder mf = new MedianFinder();
// mf.addNum(1);
// mf.findMedian();
class MedianFinder {
Queue<Integer> minPQ = new PriorityQueue<>();
Queue<Integer> maxPQ = new PriorityQueue<>(10, (Integer i1, Integer i2) -> i2 - i1);
// Adds a number into the data structure.
public void addNum(int num) {
minPQ.offer(num);
maxPQ.offer(minPQ.poll());
if (minPQ.size() < maxPQ.size()) minPQ.offer(maxPQ.poll());
} // Returns the median of current data stream
public double findMedian() {
if (minPQ.size() == maxPQ.size()) return (minPQ.peek() + maxPQ.peek()) / 2.0;
return minPQ.peek();
}
}; // Your MedianFinder object will be instantiated and called as such:
// MedianFinder mf = new MedianFinder();
// mf.addNum(1);
// mf.findMedian();
												

[LeetCode] 295. Find Median from Data Stream ☆☆☆☆☆(数据流中获取中位数)的更多相关文章

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

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

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

  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. 295 Find Median from Data Stream 数据流的中位数

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

  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. mail命令

    mail命令是命令行的电子邮件发送和接收工具.操作的界面不像elm或pine那么容易使用,但功能非常完整. 语法 mail(选项)(参数) 选项 -b<地址>:指定密件副本的收信人地址: ...

  2. 次短路——Dijkstra

    传送门 ——在LYC大佬的帮助下过了这道题 思路: LYC大佬的博客里已经讲得很清晰了,我只是提一下要点. 求次短路,主要考虑两个方面: ①在不重复走一条路的前提下,把最短路的其中一段替换为另一段. ...

  3. 阿里云CentOS Linux服务器上搭建邮件服务器遇到的问题

    参考文章: 阿里云CentOS Linux服务器上用postfix搭建邮件服务器 Linux系统下邮件服务器的搭建(Postfix+Dovecot) 本来想自己搭建邮件服务器,但是看到一篇资料表示阿里 ...

  4. STL_头文件

    #include <string> #include <vector> #include <deque> #include <queue> #inclu ...

  5. [转][osg]关于PagedLOD 加载卸载机制

    你的PagedLOD 为什么没有卸载 转自:http://bbs.osgchina.org/forum.php?mod=viewthread&tid=7612&highlight=Pa ...

  6. Node.js 常用命令

    1. 查看node版本 node --version 2. 查看npm 版本,检查npm 是否正确安装. npm -v 3. 安装cnpm (国内淘宝镜像源),主要用于某些包或命令程序下载不下来的情况 ...

  7. 神兽保佑代码无bug O(∩_∩)O

    <!DOCTYPE html><html> <head> <meta charset="UTF-8"> <title>& ...

  8. zabbix自定义监控方式

  9. boke练习: spring boot: security post数据时,要么关闭crst,要么添加隐藏域

    spring boot: security post数据时,要么关闭crst,要么添加隐藏域 http.csrf().disable(); 或者: <input name="${_cs ...

  10. 使用Service组件实现简单的音乐播放器功能 --Android基础

    1.本例利用Service实现简单的音乐播放功能,下面是效果图.(点击开始播放开启服务,音乐播放,点击“停止播放”关闭服务,音乐停止播放.) 2.核心代码: MusicService.java: pa ...