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 ...
随机推荐
- 符号表实现(Symbol Table Implementations)
符号表的实现有很多方式,下面介绍其中的几种. 乱序(未排序)数组实现 这种情况,不需要改变数组,操作就在这个数组上执行.在最坏的情况下插入,搜索,删除时间复杂度为O(n). 有序(已排序)数组实现 这 ...
- 《Learn python the hard way》Exercise 48: Advanced User Input
这几天有点时间,想学点Python基础,今天看到了<learn python the hard way>的 Ex48,这篇文章主要记录一些工具的安装,以及scan 函数的实现. 首先与Ex ...
- 获取java类和方法名
String clazz = this.getClass().getName(); String method = Thread.currentThread() .getStackTrace()[1] ...
- alert 在手机浏览器会显示网址,怎么能去掉这个网址
之前就看到有人发过这帖子,现在自己也遇到这问题了. 目前想到的一个解决方案,是用jquery的模拟的alert插件进行代替,可是找的几个插件都不能实现alert的阻塞功能.怎么破 ,具体解决方案如下: ...
- Phoenix二级索引(Secondary Indexing)的使用
摘要 HBase只提供了一个基于字典排序的主键索引,在查询中你只能通过行键查询或扫描全表来获取数据,使用Phoenix提供的二级索引,可以避免在查询数据时全表扫描,提高查过性能,提升查询效率 测试 ...
- 压位加速-poj-2443-Set Operation
题目链接: http://poj.org/problem?id=2443 题目意思: 有n个集合(n<=1000),每个集合有m个数ai(m<=10000,1=<ai<=100 ...
- 畅通project
原文请訪问:p=174">http://xiaoshig.sinaapp.com/?p=174 畅通project Time Limit:2000MS Memory Limit ...
- ssh命令
使用ssh命令登陆远程系统 ssh [ip/address] -l [登陆用户名] 如: ssh www.xyz.cn -l root
- ubuntu16.04 server安装小记
由于本人有一台闲置的thinkpad电脑,所以打算在上边安装一个ubuntu16.04 server版本,其中遇到主要问题,做一下记录: 安装过程中出现“ubuntu16.04 server64 bu ...
- 如何把一个表中的部分字段值插入到另一个表中去 这sql吊
Insert into JHAC_TB_CODE(CID,CODE,ADD_TIME,USERID,PRO_CODE,USERNAME) select f_code.FID,f_code.Fcod ...