[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 二.题目大意: 给定一段数据流,要求求出数据流中的中位数,其中数据 ...
随机推荐
- openlayers的loaders方式加载
openlayers loaders方式加载 let layerVector = new ol.layer.Vector({ source : new ol.source.Vector({ loade ...
- Python笔记_第一篇_面向过程_第一部分_5.Python数据类型之数字类型(number)
Python 数字类型(number)用于存储数值.数据类型是不允许改变的,这就意味着如果改变number数据类型的值,将重新分配内存空间. 1. 一个简单的示例: # 以下实例在变量赋值时数字类 ...
- 提示Asp.net4.5未在web服务器上注册
在用vs2012代开项目时, 每回都显示Asp.net4.5未在web服务器上注册,最后是由于没有下载一个补丁的原因,只需要下载安装补丁 VS11-KB3002339.exe ,下载地址:https: ...
- HashMap看这篇就够了
HashMap看这篇就够了 一文读懂HashMap Java8容器源码-目录
- Java之线程通信的方法
/** * 线程通信的例子:使用两个线程打印 1-100.线程1, 线程2 交替打印 * * 涉及到的三个方法: * wait():一旦执行此方法,当前线程就进入阻塞状态,并释放同步监视器. * no ...
- scrapy补充-分布式爬虫
spiders 介绍:在项目中是创建爬虫程序的py文件 #1.Spiders是由一系列类(定义了一个网址或一组网址将被爬取)组成,具体包括如何执行爬取任务并且如何从页面中提取结构化的数据. #2.换句 ...
- Idea创建Spring项目
环境 win7 + Idea2018 Classpath commons-logging-1.2 + spring-framework-4.1.6.RELEASE Step1 创建工程 File -& ...
- Linux(CENTOS7) Mysql不能远程连接解决办法
今天,在腾讯云的服务器上面装了一个Mysql,装完发现我在linux下面可以连接,但是在我的window下面是用mysql可视化工具(SQLyog)连接不了,错误如下: Host ‘’ is not ...
- arg min,arg max, e.g ,i.e
数学中常见的arg min,arg max 是什么意思 arg 是变元(即自变量argument)的英文缩写 arg min 就是使后面这个式子到达最小值时的变量的取值 arg max 就是使后面这个 ...
- POJ 3273 Monthly Expense二分查找[最小化最大值问题]
POJ 3273 Monthly Expense二分查找(最大值最小化问题) 题目:Monthly Expense Description Farmer John is an astounding a ...