题目:

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

分析:

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

很明显我们最好不要每调用一次求中位数函数就重新计算一遍,这样做时间复杂度较高。

剑指offer中有一道相同的题目,可以参考这篇理解这道题,剑指Offer-63.数据流中的中位数(C++/Java)

程序:

C++

class MedianFinder {
public:
/** initialize your data structure here. */
MedianFinder() {
index = 0;
} void addNum(int num) {
if(index % 2 == 0){
minHeap.push(num);
maxHeap.push(minHeap.top());
minHeap.pop();
}
else{
maxHeap.push(num);
minHeap.push(maxHeap.top());
maxHeap.pop();
}
index++;
} double findMedian() {
double res = 0;
if(index % 2 == 0){
res = (double)(maxHeap.top() + minHeap.top()) / 2;
return res;
}
else{
res = (double)maxHeap.top();
return res;
}
}
private:
priority_queue <int, vector<int>, less<int> > maxHeap;
priority_queue <int, vector<int>, greater<int> > minHeap;
int index;
};

Java

class MedianFinder {

    /** initialize your data structure here. */
public MedianFinder() {
minHeap = new PriorityQueue<Integer>();
maxHeap = new PriorityQueue<Integer>(11,new Comparator<Integer>(){
@Override
public int compare(Integer i1,Integer i2){
return i2-i1;
}
});
index = 0;
} public void addNum(int num) {
if(index % 2 == 0){
minHeap.offer(num);
maxHeap.offer(minHeap.poll());
}
else{
maxHeap.offer(num);
minHeap.offer(maxHeap.poll());
}
index++;
} public double findMedian() {
double res = 0;
if(index % 2 == 0){
res = (minHeap.peek() + maxHeap.peek()) / 2.0;
return res;
}
else{
res = maxHeap.peek();
return res;
}
}
private PriorityQueue<Integer> minHeap;
private PriorityQueue<Integer> maxHeap;
private int index;
}

LeetCode 295. Find Median from Data Stream数据流的中位数 (C++/Java)的更多相关文章

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

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

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

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

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

  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/ Median is the middle value in an ordered ...

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

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

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

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

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

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

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

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

随机推荐

  1. 力扣349(java&python)-两个数组的交集(简单)

    题目: 给定两个数组 nums1 和 nums2 ,返回 它们的交集 .输出结果中的每个元素一定是 唯一 的.我们可以 不考虑输出结果的顺序 . 示例 1: 输入:nums1 = [1,2,2,1], ...

  2. ADBPG&Greenplum成本优化之磁盘水位管理

    ​简介:本文我们将通过一个实际的磁盘空间优化案例来说明,如何帮助客户做成本优化. ​ 作者 | 玉翮 来源 | 阿里技术公众号 一 背景描述 目前,企业的核心数据一般都以二维表的方式存储在数据库中.在 ...

  3. 基于 MaxCompute + Hologres 的人群圈选和数据服务实践

    ​简介: 本文主要介绍如何通过 MaxCompute 进行海量人群的标签加工,通过 Hologres 进行分析建模,从而支持大规模人群复杂圈选场景下的交互式体验,以及基于API的数据服务最佳实践. 本 ...

  4. PostgreSQL数据目录深度揭秘

    简介: PostgreSQL是一个功能非常强大的.源代码开放的客户/服务器关系型数据库管理系统(RDBMS),被业界誉为"先进的开源数据库",支持NoSQL数据类型,主要面向企业复 ...

  5. [Tools] Kali Linux 高清屏扩大系统字体、BurpSuite、OpenVAS

    系统检索 Setting Manager,Appearance -> Settings,选择 Window Scaling:2x Terminal fonts: linux terminal 快 ...

  6. Django之路由层、视图层、模板层介绍

    一.Django请求生命周期 1.路由层urls.py Django 1.11版本 URLConf官方文档 1.1 urls.py配置基本格式 from django.conf.urls import ...

  7. Ubuntu安装完VMware tools还是不能和主机之间拖拽文件

    Ubuntu安装完VMware tools还是不能和主机之间拖拽文件 1.确保已安装了VMware Tools 2.禁用 Wayland sudo gedit /etc/gdm3/custom.con ...

  8. ssh秘钥对免密码登陆

    准备两台linux服务器 a和b , 在a上使用ssh命令登陆b服务器 , 并且不用 输入密码 1.在a服务器上,比如是root用户 ,进去/root/.ssh目录 ,没有就创建, 就是进入家目录的. ...

  9. ansible(7)--ansible的file模块

    1. file模块 功能:为被控端创建文件或目录,设定权限属性: 主要参数如下: 参数 说明 path 指定远程服务器的路径,也可以写成'dest','name' state 状态,可以将值设定为di ...

  10. tomcat(2)- tomcat目录结果和配置文件

    目录 1 Tomcat目录结构 2 Tomcat的配置文件 2.1 server.xml配置文件 2.2 server.xml配置文件结构 2.3 WEB应用自动部署 2.4 配置文件各个组件的关联 ...