https://leetcode.com/problems/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:
priority_queue<int,vector<int>, greater<int> > maxHeap;
priority_queue<int> minHeap; public: // Adds a number into the data structure.
void addNum(int num) {
if(minHeap.empty() || num <= minHeap.top()){
if(minHeap.size() > maxHeap.size()){
maxHeap.push(minHeap.top());
minHeap.pop();
}
minHeap.push(num);
}
else if(maxHeap.empty() || num > maxHeap.top()){
if(maxHeap.size() > minHeap.size()){
minHeap.push(maxHeap.top());
maxHeap.pop();
}
maxHeap.push(num);
}
else{
if(maxHeap.size() >= minHeap.size()) minHeap.push(num);
else if(minHeap.size() > maxHeap.size()) maxHeap.push(num);
}
} // Returns the median of current data stream
double findMedian() {
if(minHeap.size() == maxHeap.size()) return (double) (minHeap.top() + maxHeap.top()) / 2.0;
else if(minHeap.size() > maxHeap.size()) return (double) minHeap.top();
else return (double) maxHeap.top();
}
}; // Your MedianFinder object will be instantiated and called as such:
// MedianFinder mf;
// mf.addNum(1);
// mf.findMedian();

leetcode@ [295]Find Median from Data Stream的更多相关文章

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

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

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

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

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

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

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

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

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

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

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

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

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

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

随机推荐

  1. ural 1233

    可以推出规律  每一个数第一次出现的位置 和 n*10后出现的位置  要特殊考虑 是10的倍数的情况(10,100,1000, .......) 它的位置是不会改变的 #include<cstd ...

  2. 如何在Ubuntu下搭建Android NDK开发环境

    1 搭建Android SDK开发环境 参考在在Ubuntu下搭建Android SDK开发环境(图文)首先在Ubuntu下搭建Android SDK开发环境. 2 下载NDK开发包 打开官网: ht ...

  3. stl map高效遍历删除的方法

    for(:iter!=mapStudent.end():) {      if((iter->second)>=aa)      {          //满足删除条件,删除当前结点,并指 ...

  4. Window Event 2008

    https://support.microsoft.com/en-us/kb/947226

  5. hdu 4035 Maze 概率DP

        题意:    有n个房间,由n-1条隧道连通起来,实际上就形成了一棵树,    从结点1出发,开始走,在每个结点i都有3种可能:        1.被杀死,回到结点1处(概率为ki)      ...

  6. hbase集群 常用维护命令

    一. zk集群 1. 查看当前服务的角色 leader/follower echo stat|nc 127.0.0.1 2181 2.  启动ZK服务: sh bin/zkServer.sh star ...

  7. jdbc操作mysql

    本文讲述2点: 一. jdbc 操作 MySQL .(封装一个JdbcUtils.java类,实现数据库表的增删改查) 1. 建立数据库连接 Class.forName(DRIVER); connec ...

  8. 【HDOJ】4605 Magic Ball Game

    思路1:树状数组+离线处理,对所有的w离散化处理,边dfs边使用树状数组更新左右w的情况.思路2:主席树,边bfs边建树.结点信息存储cnt,然后在线查询.树状数组. /* 4605 */ #incl ...

  9. unity3d中获得物体的尺寸(size)

    1:获得诸如Plane.Cube的size.    1):可以为它们添加Collider,然后使用XXX.collider.bounds.size;该方法获得的size和缩放比例有关,是一一对应的,缩 ...

  10. mac osx 升级yosemite后java出错的解决

    原文  http://www.cnblogs.com/walkerwang/p/4034152.html