[OJ] Data Stream Median (Hard)
LintCode 81. Data Stream Median (Hard)
思路:
- 用一个大根堆保存较小的一半数, 一个小根堆保存较大的一半数.
 - 每次根据
num和两个堆顶的数据决定往哪个堆里面放. - 放完后进行平衡确保两个堆的
size差不超过1. - 利用两个堆的
size和堆顶值计算median. 
大根堆可以表示为priority_queue<int, vector<int>, less<int>>, 其实priority_queue<int>默认就是大根堆.
小根堆可以表示为priority_queue<int, vector<int>, greater<int>>.
class Solution {
public:
    vector<int> medianII(vector<int> &nums) {
        priority_queue<int, vector<int>, less<int>> sm;
        priority_queue<int, vector<int>, greater<int>> gt;
        vector<int> v;
        for (int n : nums) {
            if (gt.empty() || n > gt.top()) {
                gt.push(n);
            } else {
                sm.push(n);
            }
            if (sm.size() > gt.size() + 1) {
                int val = sm.top();
                sm.pop();
                gt.push(val);
            }
            if (gt.size() > sm.size() + 1) {
                int val = gt.top();
                gt.pop();
                sm.push(val);
            }
            if (gt.size() > sm.size()) {
                v.push_back(gt.top());
            } else {
                v.push_back(sm.top());
            }
        }
        return v;
    }
};
LeetCode 295. Find Median from Data Stream (Hard)
class MedianFinder {
private:
    priority_queue<int, vector<int>, less<int>> sm;
    priority_queue<int, vector<int>, greater<int>> gt;
public:
    // Adds a number into the data structure.
    void addNum(int num) {
        if (gt.empty() || num > gt.top()) {
            gt.push(num);
        } else {
            sm.push(num);
        }
    }
    // Returns the median of current data stream
    double findMedian() {
        while (sm.size() > gt.size() + 1) {
            int val = sm.top();
            sm.pop();
            gt.push(val);
        }
        while (gt.size() > sm.size() + 1) {
            int val = gt.top();
            gt.pop();
            sm.push(val);
        }
        if (gt.size() > sm.size()) {
            return gt.top();
        } else if (sm.size() > gt.size()) {
            return sm.top();
        } else {
            return (gt.top() + sm.top()) / 2.0;
        }
    }
};
时间复杂度: O(nlogn)
空间复杂度: O(n)
[OJ] Data Stream Median (Hard)的更多相关文章
- lintcode 1: Data Stream Median
		
Data Stream Median Numbers keep coming, return the median of numbers at every time a new number adde ...
 - LintCode "Sliding Window Median" & "Data Stream Median"
		
Besides heap, multiset<int> can also be used: class Solution { void removeOnly1(multiset<in ...
 - 数据流中位数 · data stream median
		
[抄题]: 数字是不断进入数组的,在每次添加一个新的数进入数组的同时返回当前新数组的中位数. [思维问题]: [一句话思路]: 左边x个元素,右边要有x+1个元素,因此利用maxheap把左边的最大值 ...
 - [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 ...
 - 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 ...
 - [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 ...
 - Find Median from Data Stream
		
常规方法 超时 class MedianFinder { vector<int> coll; public: MedianFinder(){ } void heapfu(vector< ...
 - 数据结构与算法(1)支线任务8——Find Median from Data Stream
		
题目如下:(https://leetcode.com/problems/find-median-from-data-stream/) Median is the middle value in an ...
 - 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 ...
 
随机推荐
- 自定义函数实现NULL值替换
			
数据库环境:SQL SERVER 2005 有时候,想将查询查询数来的数据格式化一下,不希望显示NULL值,或者复制表的数据时,被插入的表不允许有NULL. 我们可以通过ISNULL()函数或者COA ...
 - MarkDown中锚点的使用
			
在文档中创建锚点: <A NAME="ROP_ON_ARM">Davi L, Dmitrienko A, Sadeghi A R, et al. [Return-ori ...
 - JavaScript DOM编程艺术 - 读书笔记1-3章
			
1.JavaScript语法 准备工作 一个普通的文本编辑器,一个Web浏览器. JavaScript代码必须通过Html文档才能执行,第一种方式是将JavaScript代码放到文档<head& ...
 - 01_Java解析XML
			
[打印list.Map集合的工具方法] /** * 打印List集合对应的元素 */ public void printList(List<Object> list){ for(Objec ...
 - KMP入门(匹配)
			
Description Given two sequences of numbers : a[1], a[2], ...... , a[N], and b[1], b[2], ...... , b[M ...
 - OpenJudge 2753 菲波那契数列
			
1.链接地址: http://bailian.openjudge.cn/practice/2753 2.题目: 总时间限制: 1000ms 内存限制: 65536kB 描述 菲波那契数列是指这样的数列 ...
 - 关于C#虚函数和构造函数的一点理解
			
虚函数感觉总是很神秘,在本质的原理上一直也没有弄得很透彻,今天又有一点的新的感悟,纪录下来,有时间的话可以去研究一下C++对象模型 using System; using System.Collect ...
 - JQuery(三) Ajax相关
			
JQuery大大简化了Ajax通用操作,开发者只需要指定请求URL,回调函数即可. 三个主要方法: $().param(obj):将obj参数(对象或数组)转化成查询字符串. {name:" ...
 - android通过泛型获取控件或视图
			
@SuppressWarnings("unchecked") public <T extends Fragment> T getFragment(int id) { T ...
 - 基于C#的SolidWorks插件开发(1)--SolidWorks API接口介绍
			
这是两年前毕业时写的一篇关于SolidWorks插件开发与公司PDM集成的毕业设计,最近闲来无事拿出来整理一下,大神们可以略过. 1.1 SolidWorks API接口 正确调用SolidWor ...