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的更多相关文章

  1. [LeetCode] 295. Find Median from Data Stream ☆☆☆☆☆(数据流中获取中位数)

    295. Find Median from Data Stream&数据流中的中位数 295. Find Median from Data Stream https://leetcode.co ...

  2. 剑指offer 最小的k个数 、 leetcode 215. Kth Largest Element in an Array 、295. Find Median from Data Stream(剑指 数据流中位数)

    注意multiset的一个bug: multiset带一个参数的erase函数原型有两种.一是传递一个元素值,如上面例子代码中,这时候删除的是集合中所有值等于输入值的元素,并且返回删除的元素个数:另外 ...

  3. 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 ...

  4. leetcode@ [295]Find Median from Data Stream

    https://leetcode.com/problems/find-median-from-data-stream/ Median is the middle value in an ordered ...

  5. [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 ...

  6. [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 ...

  7. 【LeetCode】295. Find Median from Data Stream 解题报告(C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 大根堆+小根堆 日期 题目地址:https://le ...

  8. 295 Find Median from Data Stream 数据流的中位数

    中位数是排序后列表的中间值.如果列表的大小是偶数,则没有中间值,此时中位数是中间两个数的平均值.示例:[2,3,4] , 中位数是 3[2,3], 中位数是 (2 + 3) / 2 = 2.5设计一个 ...

  9. LeetCode——295. Find Median from Data Stream

    一.题目链接: https://leetcode.com/problems/find-median-from-data-stream 二.题目大意: 给定一段数据流,要求求出数据流中的中位数,其中数据 ...

随机推荐

  1. 总结一些常用的训练 GANs 的方法

    众所周知,GANs 的训练尤其困难,笔者自从跳入了 GANs 这个领域(坑),就一直在跟如何训练 GANs 做「对抗训练」,受启发于 ganhacks,并结合自己的经验记录总结了一些常用的训练 GAN ...

  2. DVWA-命令执行

    开门见山 ·Low ·Medium ·High · 命令执行监听端口 ;mkfifo /tmp/pipe;sh /tmp/pipe | nc -nlp 4444 > /tmp/pipe nc 1 ...

  3. 配置Action

    配置Action 实现了Action类后,就可以在struts.xml中配置该Action类了.配置Action就是让Struts2 知道哪个Action处理哪个请求,也就是完成用户请求和Action ...

  4. 对于centos的运用ssh远程连接

    1,首先安装ssh服务器 $yum install openssh-server 2,记录你当前centos的ip地址 $ifconfig 3,再在windows里面安装putty 4安装完成后, 在 ...

  5. Java常用面试题总结

    1.多线程实现方式 2.cookie和session区别 3.数据加密 4.接口并发 5.常用的集合类 6.遍历集合方式 7.接口和抽象类 8.#和$区别 9.防止sql注入 10.springMvc ...

  6. MySQL--事务控制和锁定语句

    MySQL 支持对 MyISAM 和 MEMORY 存储引擎的表进行表级锁定,对 BDB 存储引擎的表进行页级锁定,对 InnoDB 存储引擎的表进行行级锁定.默认情况下,表锁和行锁都是自动获得的,不 ...

  7. diverta 2019 Programming Contest 2自闭记

    A 签到(a-b problem不用贴了吧,以后atcoder小于300分题均不贴代码) B 发现选择的p,q一定是其中两点间的距离,于是可以O(n2)枚举两点,再O(n2)判断,其实可以做到O(n3 ...

  8. 自动按键的Sendkeys工具的下载和使用

    大家好! 下面介绍一款自动按键的小工具:Sendkeys 下载地址 Sendkeys.rar 按键脚本的书写规则如下: 启动本工具后,在工具中打开一个脚本文件,然后在工具中按下Ctrl+A全选所有脚本 ...

  9. request请求生命周期

    request请求生命周期 一.request请求分析 1.1. request数据请求 # views.py from rest_framework.views import APIView fro ...

  10. 对Spring aware理解

    aware翻译过来时就是意识到,我对他的理解就是spring的感知器.是不是很诡异这个名字起得^_^ 先来看看aware接口的结构 spring提供了许多的aware,Aware.java也只是做一个 ...