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. BZOJ 4247: 挂饰 题解

    Description JOI君有N个装在手机上的挂饰,编号为1...N. JOI君可以将其中的一些装在手机上. JOI君的挂饰有一些与众不同--其中的一些挂饰附有可以挂其他挂件的挂钩.每个挂件要么直 ...

  2. powerdesigner12.5入门教程

    2015年1月23日21:13:43|  分类: 默认分类 |  标签: |字号大中小 订阅 转自http://blog.163.com/aijingyuan_198814/blog/static/1 ...

  3. [译]GLUT教程 - glutPostRedisplay函数

    Lighthouse3d.com >> GLUT Tutorial >> Avoiding the Idle Func >> glutPostRedisplay 直 ...

  4. Windows 8.1 应用再出发 (WinJS) - 几种新增控件(1)

    Windows 8.1 和 WinJS 引入了以下新控件和功能,分别是:AppBarCommand.BackButton.Hub.ItemContainer.NavBar.Repeater.WebVi ...

  5. [ACM] poj 1064 Cable master (二分查找)

    Cable master Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 21071   Accepted: 4542 Des ...

  6. Linux内核分析之操作系统是如何工作的

    在本周的课程中,孟老师主要讲解了操作系统是如何工作的,我根据自己的理解写了这篇博客,请各位小伙伴多多指正. 一.知识点总结 1. 三个法宝 存储程序计算机:所有计算机基础性的逻辑框架. 堆栈:高级语言 ...

  7. OpenGL(三)——函数大全

    概述 根据自己写的小程序对各个函数进行解释 OpenGL函数 1. 颜色 1.1  glShadeModel 绘制指定两点间其他点颜色的过渡模式 没试 1.2  glColor 设置当前颜色:可以派生 ...

  8. Webpack使用教程一

    过去数年间,web开发已经从包含少量JavaScript代码的应用发展到到拥有复杂JavaScript代码和代码之间依赖关系树的应用.手动维护这些复杂的代码依赖关系是很麻烦的.Webpack能分析项目 ...

  9. [ASE][Daily Scrum]11.26

    今天主要是修复一些历史遗留问题以及bug, 在task上进展不是很明显, 在地图与客户端同步之后产生了一些bug,例如一发子弹会消掉很多砖块,服务器地图与本地地图不同步等等... 目前的大方向分工是这 ...

  10. JSP标准标签库(JSTL)之核心标签(上)

    在myeclipse中新建web项目时,会自动为我们安装JSTL库,如下图: 核心标签是我们最常用的JSTL标签.在JSP页面中引用核心标签库的语法如下: <%@ taglib uri=&quo ...