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
链接: http://leetcode.com/problems/find-median-from-data-stream/
题解:
在Data stream中找到median。这道题是Heap的经典应用,需要同时维护一个最大堆和一个最小堆, 最大堆和最小堆的size <= 当前数字count / 2。在学习heap数据结构的时候一般都会讲到这一题,很经典。
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();
二刷:
依然使用了两个PriorityQueue,一个maxPQ,一个minPQ。最小堆中存的是较大的一半数据,最大堆中存的是较小的一半数据。当传入数字计数为奇数是,我们直接返回minPQ.peek(), 否则我们返回 (maxPQ.peek() + minPQ.peek()) / 2.0。 这样做速度不是很理想,也许是maxPQ的lambda表达式构建出的comparator没有得到优化,换成普通的comparator速度至少快一倍。也可以用Collections.reverseOrder()来作为maxPQ的comparator。
见到有些朋友用了构建BST来做,这样减少了几次O(logn)的操作,速度会更快,以后在研究。
Java:
Min and Max Heap
Time Complexity: addNum - O(logn) , findMedian - O(1), Space Complexity - O(n)
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();
Reference:
https://leetcode.com/discuss/65107/share-my-java-solution-logn-to-insert-o-1-to-query
https://leetcode.com/discuss/64850/short-simple-java-c-python-o-log-n-o-1
https://leetcode.com/discuss/64811/easy-to-understand-double-heap-solution-in-java
https://leetcode.com/discuss/64910/very-short-o-log-n-o-1
https://leetcode.com/discuss/64842/32ms-easy-to-understand-java-solution
https://leetcode.com/discuss/68290/simple-java-solution-with-2-heaps-and-explanation
https://leetcode.com/discuss/64852/java-python-two-heap-solution-o-log-n-add-o-1-find
https://leetcode.com/discuss/94126/18ms-beats-100%25-java-solution-with-bst
295. Find Median from Data Stream的更多相关文章
- [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@ [295]Find Median from Data Stream
https://leetcode.com/problems/find-median-from-data-stream/ Median is the middle value in an ordered ...
- [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 找出数据流的中位数
Median is the middle value in an ordered integer list. If the size of the list is even, there is no ...
- [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 ...
- 【LeetCode】295. Find Median from Data Stream 解题报告(C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 大根堆+小根堆 日期 题目地址:https://le ...
- 295 Find Median from Data Stream 数据流的中位数
中位数是排序后列表的中间值.如果列表的大小是偶数,则没有中间值,此时中位数是中间两个数的平均值.示例:[2,3,4] , 中位数是 3[2,3], 中位数是 (2 + 3) / 2 = 2.5设计一个 ...
- LeetCode——295. Find Median from Data Stream
一.题目链接: https://leetcode.com/problems/find-median-from-data-stream 二.题目大意: 给定一段数据流,要求求出数据流中的中位数,其中数据 ...
随机推荐
- 表格实现hao123
一.表格实现hao123用到的标签元素 1.[width][bordercolor][cellpadding][rules="none"隐藏表格内线框][border] 例如: & ...
- Node.js 学习(五)Node.js 事件循环
Node.js 是单进程单线程应用程序,但是通过事件和回调支持并发,所以性能非常高. Node.js 的每一个 API 都是异步的,并作为一个独立线程运行,使用异步函数调用,并处理并发. Node.j ...
- 前端之JavaScript第四天学习(10)-JavaScript-运算符
运算符 = 用于赋值. 运算符 + 用于加值. 运算符 = 用于给 JavaScript 变量赋值. 算术运算符 + 用于把值加起来. y=5; z=2; x=y+z; 在以上语句执行后,x 的值是 ...
- 我是IT小小鸟,老师我之前上传了!!!!老是请您看到我
我是IT小小鸟读后感 世界上没有一蹴而就的成功者,只有头悬梁锥刺股的奋斗者.蜉蝣向往大鹏的辉煌,却不曾知大鹏以往的汗水.蜉蝣之所以为蜉蝣,是因为它犹如井底之蛙,目光短浅,之盲目地羡慕成功者,而大鹏之所 ...
- 通过Log4j的DEBUG级别来显示mybatis的sql语句
为了更加方便调试sql语句,需要显示mybatis的sql语句. 网络上的一般方式都是通过log4j来实现,但是很多都有问题. 经过实验,以下代码能够保持正常:(只显示myb ...
- 01.Redis安装
1.安装Redis 1.下载.解压Redis [lizhiwei@localhost Redis]$ ll total 1248 -rwxrwxr-x. 1 lizhiwei lizhiwei 127 ...
- 【BZOJ】【2301】problem b
莫比乌斯反演/容斥原理 Orz PoPoQQQ PoPoQQQ莫比乌斯函数讲义第一题. for(i=1;i<=n;i=last+1){ last=min(n/(n/i),m/(m/i)); …… ...
- JAVA数据结构系列 栈
java数据结构系列之栈 手写栈 1.利用链表做出栈,因为栈的特殊,插入删除操作都是在栈顶进行,链表不用担心栈的长度,所以链表再合适不过了,非常好用,不过它在插入和删除元素的时候,速度比数组栈慢,因为 ...
- 16进制转rgb
- (void)colorWithHexString: (NSString *) stringToConvert { //去掉前后空格换行符 NSString *cString = [[stringT ...
- ASP.NET中数据棒图,饼图,柱状图的实现
Web中绘制图形的方法大致有: 1. VML方式:功能强大,但是非常麻烦. 推荐:http://www.elook.net.cn/vml/ 2.使用控件:Dandus, Aspose.chart,Co ...