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 {
private:
multiset<int> left, right; public: // Adds a number into the data structure.
void addNum(int num) {
if (right.empty() || num < *right.begin()) left.insert(num);
else right.insert(num);
} // Returns the median of current data stream
double findMedian() {
int size = left.size() + right.size();
while (left.size() > size / ) {
int tmp = *left.rbegin();
right.insert(tmp);
left.erase(left.find(tmp));
}
while (left.size() < size / ) {
int tmp = *right.begin();
left.insert(tmp);
right.erase(right.find(tmp));
}
if (size & ) return *right.begin();
else return (double)(*left.rbegin() + *right.begin()) / ;
}
}; // Your MedianFinder object will be instantiated and called as such:
// MedianFinder mf;
// mf.addNum(1);
// mf.findMedian();

[LeetCode] Find Median from Data Stream的更多相关文章

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

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

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

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

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

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

  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 找出数据流的中位数

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

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

随机推荐

  1. javascript 对象属性的get set访问器写法

    function Person() {     var age = new Date().getFullYear() - 18;     Object.defineProperty(this, &qu ...

  2. python基础语法(4)

    十.Python标准库 Python标准库是随Pthon附带安装的,包含了大量极其有用的模块. 1. sys模块 sys模块包含系统对应的功能 sys.argv ---包含命令行参数,第一个参数是py ...

  3. 『TCP/IP详解——卷一:协议』读书笔记——06

    2013-08-20 14:41:01 2.8 最大传输单元MTU MTU,最大传输单元:以太网和802.3对数据帧的长度都有一个限制,其最大值分别是1500和1492字节.这个不同网络对应的传输上限 ...

  4. BZOJ3171 Tjoi2013 循环格

    传送门 Description 一个循环格就是一个矩阵,其中所有元素为箭头,指向相邻四个格子.每个元素有一个坐标(行,列),其中左上角元素坐标为(0,0).给定一个起始位置(r,c) ,你可以沿着箭头 ...

  5. mysql 语句执行顺序问题

    今天在写程序的时候,做分页查找时无意中,将计算数据库查询数量的语句,放到了limit之中,导致出现了bug. 所以发现以下问题: select count(1) from table limit 0, ...

  6. I535卡刷土豆修改4.1.2版本ROMV4過程

    I535卡刷土豆修改版本ROMV4過程 一.首先在电脑上安装I535的电脑驱动程序 二.備份EFS,備份舊ROM(网上有教程) 三.解锁:下载EZ-Unlock解锁. 四.檢查Recovery是否為最 ...

  7. p7 struct and union

    struct  StudentRec           //①声明结构体类型StudentRec{        char StuNum[20];        //②定义结构体的成员变量      ...

  8. 用c#开发微信 (12) 微统计 - 阅读分享统计系统 2 业务逻辑实现

    微信平台自带的统计功能太简单,有时我们需要统计有哪些微信个人用户阅读.分享了微信公众号的手机网页,以及微信个人用户访问手机网页的来源:朋友圈分享访问.好友分享消息访问等.本系统实现了手机网页阅读.分享 ...

  9. 重新延时运行的Js 实现

    场景 1. AutoComplete 插件, 当用户的输入空闲0.5s 时,才向服务发送请求.而不是用户输入每一个字符都要请求服务器. 2. 图片懒加载时,用户拖动滚动条空闲0.5s时,才遍历懒加载的 ...

  10. DFD数据流程图

    顶层图DFD 0层图 1层图