Find Median from Data Stream 解答
Question
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
Solution 1 -- Heap
我们可以依据这个思路来想这道题:
median -> 中位数 -> 中位数两边的subarrays的大小差不超过1 -> 什么数据结构可以满足实时地将输入数组排序,并且保持一半一半的大小?
第一选择是Heap
我们维护一个MaxHeap用来存前半段的小的数据,维护一个MinHeap用来存后半段的大的数据
每次加入一个新数字,我们进行如下顺序判断:
1. MaxHeap是否是空,如果是,加入MaxHeap
2. num是否小于MaxHeap的最大值,如果是,加入MaxHeap
3. MinHeap是否是空,如果是,加入MinHeap
4. MaxHeap和MinHeap都不空
num 和 MaxHeap的最大值 和 MinHeap的最小值 比较
class MedianFinder {
private PriorityQueue<Integer> minHeap;
private PriorityQueue<Integer> maxHeap;
private Double median;
public MedianFinder() {
minHeap = new PriorityQueue<Integer>(11);
maxHeap = new PriorityQueue<Integer>(11, Collections.reverseOrder());
median = null;
}
// Adds a number into the data structure.
public void addNum(int num) {
int size1 = maxHeap.size();
int size2 = minHeap.size();
if (size1 == 0) {
maxHeap.add(num);
} else if (num <= maxHeap.peek()) {
maxHeap.add(num);
} else if (size2 == 0) {
minHeap.add(num);
}else {
int firstMax = maxHeap.peek();
int secondMin = minHeap.peek();
if (num <= firstMax) {
maxHeap.add(num);
} else if (num >= secondMin) {
minHeap.add(num);
} else{
maxHeap.add(num);
}
}
// Check balance
size1 = maxHeap.size();
size2 = minHeap.size();
if (size1 > size2 + 1) {
while (size1 > size2 + 1) {
int top = maxHeap.poll();
minHeap.offer(top);
size1--;
size2++;
}
} else if (size2 > size1 + 1) {
while (size2 > size1 + 1) {
int top = minHeap.poll();
maxHeap.offer(top);
size2--;
size1++;
}
}
if (size2 == size1 + 1)
median = (double) minHeap.peek();
if (size1 == size2 + 1)
median = (double) maxHeap.peek();
if (size1 == size2 && size1 > 0)
median = ((double) maxHeap.peek() + (double) minHeap.peek()) / 2;
}
// Returns the median of current data stream
public double findMedian() {
return median.doubleValue();
}
};
// Your MedianFinder object will be instantiated and called as such:
// MedianFinder mf = new MedianFinder();
// mf.addNum(1);
// mf.findMedian();
Solution 2 -- Balanced BST
平衡二叉树也可以满足条件,但是实际代码太多。所以可以在面试时优先用heap写出代码,然后follow-up的时候提出平衡二叉树的思想。
Find Median from Data Stream 解答的更多相关文章
- [LeetCode] Find Median from Data Stream
Find Median from Data Stream Median is the middle value in an ordered integer list. If the size of t ...
- [LeetCode] 295. Find Median from Data Stream ☆☆☆☆☆(数据流中获取中位数)
295. Find Median from Data Stream&数据流中的中位数 295. Find Median from Data Stream https://leetcode.co ...
- 剑指offer 最小的k个数 、 leetcode 215. Kth Largest Element in an Array 、295. Find Median from Data Stream(剑指 数据流中位数)
注意multiset的一个bug: multiset带一个参数的erase函数原型有两种.一是传递一个元素值,如上面例子代码中,这时候删除的是集合中所有值等于输入值的元素,并且返回删除的元素个数:另外 ...
- [LeetCode] 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 ...
- Find Median from Data Stream
常规方法 超时 class MedianFinder { vector<int> coll; public: MedianFinder(){ } void heapfu(vector< ...
- 数据结构与算法(1)支线任务8——Find Median from Data Stream
题目如下:(https://leetcode.com/problems/find-median-from-data-stream/) Median is the middle value in an ...
- LeetCode——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 ...
- 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 ...
- leetcode@ [295]Find Median from Data Stream
https://leetcode.com/problems/find-median-from-data-stream/ Median is the middle value in an ordered ...
随机推荐
- http与https的区别以及https的加密原理
HTTPS(Secure Hypertext Transfer Protocol)安全超文本传输协议 它是一个安全通信通道,它基于HTTP开发,用于在客户计算机和服务器之间交换信息.它使用安全套接字层 ...
- asp.net 中的错误跳转 customerrors 对html文件不起作用
在配置web.config时发现customerrors对aspx文件是起作用的,我想通过customerrors来判断是否有html文件时,却不起作用? 这是为什么,如果要起作用.net里该如何操作 ...
- poj 1486 Sorting Slides(二分图匹配的查找应用)
Description Professor Clumsey is going to give an important talk this afternoon. Unfortunately, he i ...
- 编程之美2015初赛第一场 hihoCoder #1156 : 彩色的树(染色问题)
#1156 : 彩色的树 时间限制:2000ms 单点时限:1000ms 内存限制:256MB 描述 给定一棵n个节点的树,节点编号为1, , …, n.树中有n - 1条边,任意两个节点间恰好有一条 ...
- Windows下文件列举,搜索
Windows下列举文件用的函数是 FindFirstFile 和 FindNextFile ,另外一个结构体是WIN32_FIND_DATA 以下是MSDN对于WIN32_FIND_DATA的定义 ...
- [IOI1999]花店橱窗布置(DP路径记录)
题目:[IOI1999]花店橱窗布置 问题编号:496 题目描述 某花店现有F束花,每一束花的品种都不一样,同时至少有同样数量的花瓶,被按顺序摆成一行,花瓶的位置是固定的,从左到右按1到V顺序编号,V ...
- [RxJS] Sharing Streams with Share
A stream will run with each new subscription added to it. This lesson shows the benefits of using sh ...
- jquery压缩图片插件
imageCompress 只有图片压缩功能,比较简单jquery.imageCompress.js 使用说明: el:为上传框 quality:压缩图片质量,单位为% onloadStart:读取图 ...
- C# 初识Ref和Out
首先:两者都是按地址传递的,使用后都将改变原来参数的数值. 其次:ref可以把参数的数值传递进函数,但是out是要把参数清空,就是说你无法把一个数值从out传递进去的,out进去后,参数的数值为空,所 ...
- (转)SQL Server2005 异常处理机制(Begin try Begin Catch)
begin try --SQL end trybegin catch --sql (处理出错动作) end catch我们将可能会出错的sql 写在begin try...end try 之间,若出 ...