[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 二.题目大意: 给定一段数据流,要求求出数据流中的中位数,其中数据 ...
随机推荐
- 不会美工的前端不是好UE
1.UE.美工.前端的工作似乎很类似,用不同的形式去画出页面效果.UE用AXURE,美工用PS,前端用代码. 2.我是一个前端,在好几家公司都是自己默默的一个人,所以在做好本职工作的同时,近朱者赤. ...
- C#——反射,自动生成添加的SQL语句
C#中的反射.是C#中特别重要也是特别神奇的特性,对后面学习框架,了解框架的原理.以及自己写框架,都是必不可少的.学习反射的过程中.总给我一种茅塞顿开的感觉,以前不懂的,现在懂了 反射的介绍:http ...
- Wallet file not specified (must request wallet RPC through /wallet/<filename> uri-path). BitcoinJSONRPCClient异常、及其他异常
1.异常信息 Wallet file not specified (must request wallet RPC through /wallet/<filename> uri-path) ...
- 3)在View中添加LBUTTONDOWN(标准消息)
1)消息一共分为四类: (1)标准消息-->以WM_ 开头的都是标准消息 (2)命令消息----> 菜单 工具条 快捷键(两个按键的组合是快捷键,一个按键是 WM_KEYDOWN( ...
- 13)编写一个子类SnakeCtrl来继承基类
1)首先是创建子类SnakeCtrl 2) 添加那个SnakeCtrl子类 3)出来了SnakeCtrl的基本样子 4)简单实现: ①改编那个SnakeCtrl.h中的内容: #pragma ...
- java 中static关键字注意事项
1.内存中存放的位置:(static修饰的方法和属性保存在方法区中,但是方法区也是堆的一部分) 内存的分区 2.什么样的属性可以定义为静态数据 例如: class person{ public Str ...
- 豆瓣爬虫Scrapy“抄袭”改写
主要是把项目从docker里面扒拉出来,但是扒拉完好像又没有什么用,放在docker里面运行多好. 源码下载下面主要记一下改动的地方吧. 配置:在database.py中改掉自己的数据库配置. 表结构 ...
- Powershell 中的管道
管道 上个命令中的输出,通过管道作为下个命令的输入.Linux中的管道传递的是text,但ps中传递的是object.但是命令究竟返回的是什么类型呢?以下命令回答了这个问题: get-service ...
- 漫谈设计模式(二):单例(Singleton)模式
1.前言 实际业务中,大多业务类只需要一个对象就能完成所有工作,另外再创建其他对象就显得浪费内存空间了,例如web开发中的servlet,这时便要用到单例模式,就如其名一样,此模式使某个类只能生成唯一 ...
- 批量刷数据sql
update t_free_m** m set m.plate_no = ( select v.plate_num from t_wh_vehi*** v where v.vin = m.car_v ...