295. 数据流的中位数

中位数是有序列表中间的数。如果列表长度是偶数,中位数则是中间两个数的平均值。

例如,

[2,3,4] 的中位数是 3

[2,3] 的中位数是 (2 + 3) / 2 = 2.5

设计一个支持以下两种操作的数据结构:

void addNum(int num) - 从数据流中添加一个整数到数据结构中。

double findMedian() - 返回目前所有元素的中位数。

示例:

addNum(1)

addNum(2)

findMedian() -> 1.5

addNum(3)

findMedian() -> 2

进阶:

如果数据流中所有整数都在 0 到 100 范围内,你将如何优化你的算法?

如果数据流中 99% 的整数都在 0 到 100 范围内,你将如何优化你的算法?

class MedianFinder {

   PriorityQueue<Integer> min ;
PriorityQueue<Integer> max ;
/** initialize your data structure here. */
public MedianFinder() {
min = new PriorityQueue<>();
max = new PriorityQueue<>((a,b) -> {return b - a ;});
} public void addNum(int num) {
max.add(num);
min.add(max.remove());
if (min.size() > max.size())
max.add(min.remove());
} public double findMedian() {
if (max.size() == min.size())
return (max.peek() + min.peek()) / 2.0;
else
return max.peek();
}
} /**
* Your MedianFinder object will be instantiated and called as such:
* MedianFinder obj = new MedianFinder();
* obj.addNum(num);
* double param_2 = obj.findMedian();
*/

Java实现 LeetCode 295 数据流的中位数的更多相关文章

  1. Leetcode 295. 数据流的中位数

    1.题目要求 中位数是有序列表中间的数.如果列表长度是偶数,中位数则是中间两个数的平均值. 例如, [2,3,4] 的中位数是 3 [2,3] 的中位数是 (2 + 3) / 2 = 2.5 设计一个 ...

  2. Java实现 LeetCode 703 数据流中的第K大元素(先序队列)

    703. 数据流中的第K大元素 设计一个找到数据流中第K大元素的类(class).注意是排序后的第K大元素,不是第K个不同的元素. 你的 KthLargest 类需要一个同时接收整数 k 和整数数组n ...

  3. Java实现 LeetCode 480 滑动窗口中位数

    480. 滑动窗口中位数 中位数是有序序列最中间的那个数.如果序列的大小是偶数,则没有最中间的数:此时中位数是最中间的两个数的平均数. 例如: [2,3,4],中位数是 3 [2,3],中位数是 (2 ...

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

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

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

  6. Java for LeetCode 216 Combination Sum III

    Find all possible combinations of k numbers that add up to a number n, given that only numbers from ...

  7. Java for LeetCode 214 Shortest Palindrome

    Given a string S, you are allowed to convert it to a palindrome by adding characters in front of it. ...

  8. Java for LeetCode 212 Word Search II

    Given a 2D board and a list of words from the dictionary, find all words in the board. Each word mus ...

  9. Java for LeetCode 211 Add and Search Word - Data structure design

    Design a data structure that supports the following two operations: void addWord(word)bool search(wo ...

随机推荐

  1. [hdu4358]树状数组

    思路:用一个数组记录最近k次的出现位置,然后在其附近更新答案.具体见代码: #pragma comment(linker, "/STACK:10240000,10240000") ...

  2. SpringData表关系:多对多

    一.编写实体类配置关联关系: 1.多对多使用注解@ManyToMany配置:a. 在实体中添加一个集合属性 b.在属性上添加ManyToMany注解 c.@JoinTable 注解配置关联关系(nam ...

  3. Codeforces 832D(Misha, Grisha and Underground,LCA)

    题意:在一棵生成树上,给出了三个点,求三个点之间最大的相交点数,CF难度1900. 题解:求出三个lca,并取深度最大的那个,就是我们要的三岔路口K,然后分别求出K到a,b,c三点的路径长度,取最大值 ...

  4. python re 里面match 和search的区别

    re.match()从开头开始匹配string. re.search()从anywhere 来匹配string. 例子: >>> re.match("c", &q ...

  5. 【雕爷学编程】Arduino动手做(59)---RS232转TTL串口模块

    37款传感器与执行器的提法,在网络上广泛流传,其实Arduino能够兼容的传感器模块肯定是不止这37种的.鉴于本人手头积累了一些传感器和执行器模块,依照实践出真知(一定要动手做)的理念,以学习和交流为 ...

  6. html,body设置高度100%后底部有空白的bug

    html,body{ //... height:100% } #app{ //... } #footbar{ position:absolute;bottom:0px;left:0px;right:0 ...

  7. WXML属性一览表

    id属性 <view id="xxx"></view> class属性 <view class="xxx"></vie ...

  8. PAT-1134 Vertex Cover (图的建立 + set容器)

    A vertex cover of a graph is a set of vertices such that each edge of the graph is incident to at le ...

  9. 【新手引导】MAC如何允许安装任何来源的软件(安装第三方软件/应用)

    1 打开系统偏好设置,找到安全性与隐私(第一行第六个) 2 进入安全性与隐私,点击左下角一个锁的按钮,输入自己电脑密码,允许从以下位置下载的应用: 新电脑应该是两个选项 1.APP store 2.A ...

  10. SecureCRT 简单运用

    1.想要从本地电脑将文件通过secureCRT传输到远程服务器如下目录 2.在SecureCRT界面中使用快捷键[Alt+P] 打开了SFTP的界面 3.找到你要传的文件的地址[E:\a.zip] 4 ...