题目:

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. 国内chatGPT中文版网站有哪些?国内人工智能百花齐放!该如何选择?

    人工智能技术在中国的快速发展和普及,使得国内的人工智能产业日益壮大.在这些领域中,自然语言处理技术和聊天机器人已经取得了显著的进展.ChatGPT作为一种基于深度学习的聊天机器人模型,在国内得到了广泛 ...

  2. 13_总结Vue数据监测

    总结: Vue监视数据的原理:         1.vue会监视data中所有层次的数据         2.如何监视对象中的数据?             通过setter实现监视,且要在new V ...

  3. 云企业网CEN-TR打造企业级私有网络

    简介: 为了满足企业大规模.多样化的组网和网络管理需求,云企业网(CEN)提出了转发路由器TR(Transit Router)的概念.在每个地域内创建一个转发路由器,可以连接大量VPC.VBR,作为您 ...

  4. 达摩院重要科技突破!空天数据库引擎Ganos解读

    简介: Ganos空天数据库引擎是李飞飞带领的达摩院数据库与存储实验室研发的新一代位置智能引擎,采用了平台即服务.多模融合.计算下推和云原生全新处理架构,为政府.企事业单位.泛互联网客户提供移动对象. ...

  5. 用手机写代码:基于 Serverless 的在线编程能力探索

    ​简介:Serverless 架构的按量付费模式,可以在保证在线编程功能性能的前提下,进一步降低成本.本文将会以阿里云函数计算为例,通过 Serverless 架构实现一个 Python 语言的在线编 ...

  6. 庖丁解牛|图解 MySQL 8.0 优化器查询转换篇

    ​简介: 本篇介绍子查询.分析表和JOIN的复杂转换过程 一  背景和架构 在<庖丁解牛-图解MySQL 8.0优化器查询解析篇>一文中我们重点介绍了MySQL最新版本8.0.25关于SQ ...

  7. WPF 已知问题 InputEventArgs 的 Timestamp 属性是静态的导致事件之间相互影响

    本文记录一个 WPF 已知的设计问题,当前此问题已经被大佬修复,这个设计问题刚好属于比较边缘的模块,我写了这么多年的代码还没有踩到这个坑一次,也没有听到有谁提到这个坑 远古时候,不知道大佬是故意还是失 ...

  8. python01-03作业

    # 小球落地,一共运动了多少米 hight = 100 # 原始高度 distance = 0 # 和 for i in range(10): # 将 下落 高度加入到 和 中 distance += ...

  9. java.lang.NoSuchMethodException: tk.mybatis.mapper.provider.base.BaseSelectProvider

    解决错误: java.lang.NoSuchMethodException: tk.mybatis.mapper.provider.base.BaseSelectProvider 整合一遍通用mapp ...

  10. Mybatis逆向工程的2种方法,一键高效快速生成Pojo、Mapper、XML,摆脱大量重复开发

    一.写在开头 最近一直在更新<Java成长计划>这个专栏,主要是Java全流程学习的一个记录,目前已经更新到Java并发多线程部分,后续会继续更新:而今天准备开设一个全新的专栏 <E ...