[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 middle value. So the median is the mean of the two middle value.
For example,
[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.
Example:
- addNum(1)
- addNum(2)
- findMedian() -> 1.5
- addNum(3)
- findMedian() -> 2
- class MedianFinder {
- private PriorityQueue<Integer> smallPq;
- private PriorityQueue<Integer> largePq;
- /** initialize your data structure here. */
- public MedianFinder() {
- // smallPq get the max value for peek()
- smallPq = new PriorityQueue<>((a, b) -> (b - a));
- largePq = new PriorityQueue<>();
- }
- public void addNum(int num) {
- if (smallPq.isEmpty() || num <= smallPq.peek()) {
- smallPq.offer(num);
- } else {
- largePq.offer(num);
- }
- if (smallPq.size() >= largePq.size() + 2) {
- largePq.offer(smallPq.poll());
- } else if (largePq.size() > smallPq.size()) {
- smallPq.offer(largePq.poll());
- }
- }
- public double findMedian() {
- if (smallPq.size() == largePq.size()) {
- return (smallPq.peek() + largePq.peek()) / 2.0;
- } else {
- return (double)(smallPq.peek());
- }
- }
- }
- /**
- * Your MedianFinder object will be instantiated and called as such:
- * MedianFinder obj = new MedianFinder();
- * obj.addNum(num);
- * double param_2 = obj.findMedian();
- */
[LC] 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函数原型有两种.一是传递一个元素值,如上面例子代码中,这时候删除的是集合中所有值等于输入值的元素,并且返回删除的元素个数:另外 ...
- 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 ...
- [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 ...
- 【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 二.题目大意: 给定一段数据流,要求求出数据流中的中位数,其中数据 ...
随机推荐
- dbus探索
一.参考网址 1.Dbus组成和原理
- HDU-3038 How Many Answers Are Wrong(带权并查集区间合并)
http://acm.hdu.edu.cn/showproblem.php?pid=3038 大致题意: 有一个区间[0,n],然后会给出你m个区间和,每次给出a,b,v,表示区间[a,b]的区间和为 ...
- LaunchPad(思维)
链接:https://ac.nowcoder.com/acm/contest/3665/D来源:牛客网 题目描述 Hery is a boy with strong practical abiliti ...
- UVA 658 状态压缩+隐式图+优先队列dijstla
不可多得的好题目啊,我看了别人题解才做出来的,这种题目一看就会做的实在是大神啊,而且我看别人博客都看了好久才明白...还是对状态压缩不是很熟练,理解几个位运算用了好久时间.有些题目自己看着别人的题解做 ...
- 201503-1 图像旋转 Java
思路: 观察输入和输出可以发现,第三列输出为第一行,第二列输出为第二行,第一列输出为第三行.循环即可 import java.util.Scanner; //得分80,本题最高需要输入100W次,因为 ...
- spring boot集成MyBatis 通用Mapper 使用总结
spring boot集成MyBatis 通用Mapper 使用总结 2019年 参考资料: Spring boot集成 MyBatis 通用Mapper SpringBoot框架之通用mapper插 ...
- poj2778 矩阵乘法+ac自动机
题:http://poj.org/problem?id=2778 题意:给定m个模式串,问长度为n的字符串不包含这些模式串的有几种可能 分析:因为n很大,所以考虑矩阵ksm来解决,构造一个矩阵res[ ...
- mysql慢SQL排查之show processlist和show full processlist
mysql排查线上数据库问题,经常会用到 show processlist和show full processlist这两条命令 processlist命令的输出结果显示了有哪些线程在运行,不仅可以查 ...
- MySQL--重定向输出内容
参考:http://www.cnblogs.com/emanlee/p/4233602.html select current_date() into outfile 'dest_path';
- JavaSE--异常信息打印
最近项目用到第三方jar包,抛出运行时异常,打在日志用的 方法.得到的错误描述并不详尽,遂想到平时用的 发现其可以重定向输出,平时用流多是和文件相关,但是在当前背景下用文件打开流显得不是很合适,翻了下 ...