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 ...
随机推荐
- 【POJ1338】Ugly Numbers(暴力打表)
打表大军是一股邪恶势力→_→ #include <iostream> #include <cstring> #include <cstdlib> #include ...
- hdu 5424 Rikka with Graph II(dfs+哈密顿路径)
Problem Description As we know, Rikka is poor at math. Yuta is worrying about this situation, so h ...
- hdu 4983 Goffi and GCD(欧拉函数)
Problem Description Goffi is doing his math homework and he finds an equality on his text book: gcd( ...
- 在Ubuntu上下载、编译和安装Android最新源代码
文章转载至CSDN社区罗升阳的安卓之旅,原文地址:http://blog.csdn.net/luoshengyang/article/details/6559955 看完了前面说的几本书之后,对Lin ...
- 浅谈GitLab与Git
前言:先解释下关于库的认识. Workspace:工作区 Index / Stage:暂存区 Repository:仓库区(或本地仓库) Remote:远程仓库 一.新增项目(远程仓库) 在GitLa ...
- sybase从表A创建表B
sybase从表A创建表B 例如:需要创建一张表B,表的内容及结构跟表A完全一样,类似于SQL SERVER中的CREATE TABLE A AS SELECT * FROM B; 在sybase中只 ...
- java基础之代理
代理的定义,代理的应用,代理的特性
- (转)sql语句中charindex的用法
假如你写过很多程序,你可能偶尔会碰到要确定字符或字符窜串否包含在一段文字中,在这篇文章中,我将讨论使用CHARINDEX和PATINDEX函数来搜索文字列和字符串.我将告诉你这两个函数是如何运转的,解 ...
- 分页搜索查询sql
select * from (select t.*,rownum no from " + table + " t where scbj=0)where (no>(" ...
- Javascript判断空对象
最近在项目开发中判断空对象时,用了“!”运算符,结果程序出现bug,找了好久才找到原因. 其实自己范了一些低级错误,现在把自己经验总结一下: 在JavaScript中,任意JavaScript的值都可 ...