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. Ubuntu16.04编译OpenCV3.4.7

    原文:https://www.bearoom.xyz/2019/08/20/ubuntu16-04-make-opencv3-4-7/ 一.前言 因为之前作死乱搞系统,然后就把Ubuntu的系统搞垮了 ...

  2. linux 解压命令总结

    常用Linux 命令: [转自]https://www.jianshu.com/p/ca41f32420d6 解压缩 .tar 解包:tar xvf FileName.tar 打包:tar cvf F ...

  3. 等和的分隔子集(dp)

    晓萌希望将 1 到 N 的连续整数组成的集合划分成两个子集合,且保证每个集合的数字和是相等. 例如,对于 N = 3,对应的集合 1, 2, 3 能被划分成3和1,2两个子集合. 这两个子集合中元素分 ...

  4. 题解 P2831 【愤怒的小鸟】

    题目 我的天,这题是真的卡精度...... 主要是精度很不好处理,经本蒟蒻测验,精度在\(10^{-6}\)会比较好优雅 [分析] 对于这种某个变量特别小\((\leq 31)\)的题目,本蒟蒻第一反 ...

  5. python中的倒序遍历

    1.在列表本身倒序 a = [1, 3, 7, 5, 2, 6] a.reverse() # 在列表本身进行倒序,不返回新的值 print(a) # 输出a: # [6, 2, 5, 7, 3, 1] ...

  6. 漫谈设计模式(三):桥接(Bridge)模式 —— 将类功能、结构两层次分离

    1.前言 类主要有两个层次,一个是功能层次,另一个是实现层次. 功能层次,一般应用于当前类不能满足多样化的业务需求,让子类去继承(具体)父类,添加加一些父类中没有的功能(一般是增加新的方法),这就属于 ...

  7. 第二季第十天 es6新特性新特性

    1.set的应用(去重)js标准内置对象 适用范围:String,Array(数字基本数据类型不可以) set的方法:例子 var s = new Set(data) 增加:  s.add(data) ...

  8. python实现图书管理系统

    # 用户注册 def logon(): print("欢迎来到图书管理系统注册页面~") username = input("请输入用户名:") if len( ...

  9. upstream(负载均衡)

    一.什么是负载均衡 负载均衡,顾名思义是指将负载尽量均衡的分摊到多个不同的服务器,以保证服务的可用性和可靠性,提供给客户更好的用户体验: 负载均衡的直接目标就是尽量发挥多个服务单元的整体效能,要实现这 ...

  10. 题解-------CF235B Let's Play Osu!

    传送门 题目大意 求出总得分的期望值. 思路 还没有学习数学期望的小朋友赶紧去学一下数学期望,这里只提供公式: $E\left ( x \right )=\sum_{k=1}^{\infty }x_{ ...