LeetCode OJ: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 middle value. So the median is the mean of the two middle value.
Examples:
[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.
For example:
add(1)
add(2)
findMedian() -> 1.5
add(3)
findMedian() -> 2
如上,可以维护一个最大堆以及一个最小堆,如果小堆数的数量大于大堆数量。那么可以把小堆中的最小值返还给大堆。(注意这个值相当于大堆中的最大者,因为小堆中的值都是大堆中转移过去的)。代码如下:
class MedianFinder {
public:
// Adds a number into the data structure.
void addNum(int num) {
maxHeap.push(num);
int tmp = maxHeap.top();
maxHeap.pop();
minHeap.push(tmp);
if(minHeap.size() > maxHeap.size()){
tmp = minHeap.top();
minHeap.pop();
maxHeap.push(tmp);
}
}
// Returns the median of current data stream
double findMedian() {
int m = maxHeap.size();
int n = minHeap.size();
if(m > n)
return maxHeap.top()/1.0;
else
return (maxHeap.top() + minHeap.top())/2.0;
}
private:
priority_queue<int, vector<int>, greater<int>> maxHeap;
priority_queue<int, vector<int>, less<int>> minHeap;
};
LeetCode OJ:Find Median from Data Stream(找数据流的中数)的更多相关文章
- [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 ...
- [LeetCode] 295. Find Median from Data Stream ☆☆☆☆☆(数据流中获取中位数)
295. Find Median from Data Stream&数据流中的中位数 295. Find Median from Data Stream https://leetcode.co ...
- [LeetCode] 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 ...
- leetcode@ [295]Find Median from Data Stream
https://leetcode.com/problems/find-median-from-data-stream/ Median is the middle value in an ordered ...
- [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 ...
- LeetCode——295. Find Median from Data Stream
一.题目链接: https://leetcode.com/problems/find-median-from-data-stream 二.题目大意: 给定一段数据流,要求求出数据流中的中位数,其中数据 ...
- [LeetCode] 346. Moving Average from Data Stream 从数据流中移动平均值
Given a stream of integers and a window size, calculate the moving average of all integers in the sl ...
- 剑指offer 最小的k个数 、 leetcode 215. Kth Largest Element in an Array 、295. Find Median from Data Stream(剑指 数据流中位数)
注意multiset的一个bug: multiset带一个参数的erase函数原型有两种.一是传递一个元素值,如上面例子代码中,这时候删除的是集合中所有值等于输入值的元素,并且返回删除的元素个数:另外 ...
- [LeetCode] Find Median from Data Stream
Find Median from Data Stream Median is the middle value in an ordered integer list. If the size of t ...
随机推荐
- VSYNC与HSYNC与PCLK与什么有关系
在手机平台,LCD,Camera,TV的接线上,都会用到PCLK,VSYNC和HSYNC这三个信号.可见这三个信号和显示关系非常大.首先我们先看这三个信号的作用: PCLK:有些方案给他起名字叫:Do ...
- Java:延迟功能的Robot在Lunix系统上会报错
Java:延迟功能的Robot在Lunix系统上会报错 关于延迟功能的Robot: 今天开发过程中发现,本机开发好的项目,部署到Lunix服务器竟然报错!查了代码发现: Robot r = new R ...
- Python3.x:遍历select下拉框获取value值
Python3.x:遍历select下拉框获取value值 Select提供了三种选择方法: # 通过选项的顺序,第一个为 0 select_by_index(index) # 通过value属性 s ...
- javascript 面向对象 new 关键字 原型链 构造函数
JavaScript面向对象JavaScript 语言使用构造函数(constructor)作为对象的模板.所谓"构造函数",就是专门用来生成实例对象的函数.它就是对象的模板,描述 ...
- spring boot加mybatis使用Map返回时,当值为空时属性也会没有(转)
使用spring boot加mybatis时,设置Map返回,当值为空时属性也会没有,就会报错 在application.properties中加入下面配置,将会解决这个问题. #当查询数据为空时 ...
- Jquery15 插件
学习要点: 1.插件概述 2.验证插件 3.自动完成插件 4.自定义插件 插件(Plugin)也成为 jQuery 扩展(Extension),是一种遵循一定规范的应用程序接口编写出来的程序.目前 j ...
- Hive查询Join
Select a.val,b.val From a [Left|Right|Full Outer] Join b On (a.key==b.key); 现有两张表:sales 列出了人名及其所购商品的 ...
- 实现ModelDriver接口的功能(转)
ModelDriver接口 来自com.opensymphony.xwork2.ModelDriven.是xwork-2.1.2-750.jar包的东西. 下面是源码: package com.ope ...
- 使用Nginx搭建图片服务器(windows)
知识点:在windows系统中,搭建图片上传服务器 参考博客:http://blog.csdn.net/u010942834/article/details/72953441 1.进入官网下载ngin ...
- springMVC注解的入门案例
1.web.xml <?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi=&qu ...