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. 腾讯一shell试题.

    腾讯一shell试题. 假设qq.tel文件内容: 12334:13510014336 12345:12334555666 12334:12343453453 12099:13598989899 12 ...

  2. 21. docker 数据通讯环境准备

    一 . 环境搭建 1.编写 Vagrantfile 并创建虚拟机 并虚拟机host绑定外部 192.168.205.10:8888 # -*- mode: ruby -*- # vi: set ft= ...

  3. setAttribute 添加属性js_jq

    属性js setAttribute document.getElementsByName("test")[0].setAttribute("src", &quo ...

  4. HDU 2586 How far away ?【LCA模板题】

    传送门:http://acm.hdu.edu.cn/showproblem.php?pid=2586 题意:给你N个点,M次询问.1~N-1行输入点与点之间的权值,之后M行输入两个点(a,b)之间的最 ...

  5. 用Pandas Dataframe来抓取重构金融股票的各种业务&数据形态

    4. 如果计算各项股票指标时,或者处理业务流程时,上一篇的直观认知数据结构,怎样帮助开发者去好好操作,又同时避免计算错误的坑. 首先从上篇的数据结据,可以看出/设计出多少种业务和股票指标. A. 恒生 ...

  6. react-native屏幕适配

    写一个屏幕适配类文件AdapterUtil.js,这样避免每次进行单位换算 "use strict" import {Dimensions, StatusBar, Platform ...

  7. Java之多线程方式一(继承Thread类)

    /** * 多线程的创建,方式一:继承于Thread类 * 1. 创建一个继承于Thread类的子类 * 2. 重写Thread类的run() --> 将此线程执行的操作声明在run()中 * ...

  8. 常用DOS命令(1) color,dir,copy,shutdown,mkdir(md),rmdir(rd),attrib,cd

    1.  color color [attr] 设置默认的控制台前景和背景颜色. attr        指定控制台输出的颜色属性.颜色属性由两个十六进制数字指定 -- 第一个对应于背景,第二个对应于前 ...

  9. 洛谷 P2278 [HNOI2003]操作系统

    题目传送门 解题思路: 一道没啥思维含量的模拟题,但是个人感觉代码实现不简单,可能是我太弱了,花了我6个小时,3次重写. AC代码: #include<iostream> #include ...

  10. 如何选择开源项目的license

    https://choosealicense.com/ http://www.csdn.net/article/2013-07-16/2816249-Github-Open-Source-Licens ...