[LeetCode] 295. Find Median from Data Stream ☆☆☆☆☆(数据流中获取中位数)
295. Find Median from Data Stream&数据流中的中位数
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.
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 ☆☆☆☆☆(数据流中获取中位数)的更多相关文章
- [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 二.题目大意: 给定一段数据流,要求求出数据流中的中位数,其中数据 ...
- [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 ...
- 295 Find Median from Data Stream 数据流的中位数
中位数是排序后列表的中间值.如果列表的大小是偶数,则没有中间值,此时中位数是中间两个数的平均值.示例:[2,3,4] , 中位数是 3[2,3], 中位数是 (2 + 3) / 2 = 2.5设计一个 ...
- 剑指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 ...
随机推荐
- JQ遇到$(‘.xxx’).attr(‘display’)一直返回undefined
jq attr && jq css 1.1 attr() 方法设置或返回被选元素的属性值 我们就题目遇到的问题做一个测试 //html <div class="div1 ...
- React创建组件的三种方式及其区别
内容转载于http://www.cnblogs.com/wonyun/p/5930333.html React推出后,出于不同的原因先后出现三种定义react组件的方式,殊途同归; 具体的三种方式: ...
- Unity Shaderlab: Object Outlines 转
转 https://willweissman.wordpress.com/tutorials/shaders/unity-shaderlab-object-outlines/ Unity Shader ...
- PostMan做接口自动化测试
try{ var jsonData = pm.response.json(); } catch (e) { console.log("No body"); } pm.environ ...
- maven 打包不同环境
支持不同环境打包 1 pom添加如下配置: 1)添加指定打包id 区分各个环境 <profiles> <profile> <id>dev</id> &l ...
- 微信小程序 数据绑定方式
与vue不同,在微信小程序中,js的数据和前端显示的数据是单数据流,也就是说,js里边的数据变了(通过setData),前端能立刻显示.但如果前端数据变了,js中的变量不能改变. 这个相比传统的前端已 ...
- 测试char,varchar存储
-- -- 表的结构 `user` -- DROP TABLE IF EXISTS `user`; CREATE TABLE IF NOT EXISTS `user` ( `id` int(11) N ...
- Codeforces 797B - Odd sum
B. Odd sum 题目链接:http://codeforces.com/problemset/problem/797/B time limit per test 1 second memory l ...
- js获取时间戳(new date()参数获取)
当获取截止到某一个时间点的时间戳时: 例如:到 2018-03-15 11:03:55 这个时间点的时间戳的时候 正确的写法: var data = new Date("2018/03/15 ...
- Access大数据高效分页语句
Access大数据高效分页语句 oracle的分页查询可以利用rowid伪列. db2的分页查询可以利用row_number() over()聚合函数. mysql有limit. access仿佛先天 ...