LintCode 81. Data Stream Median (Hard)

思路:

  1. 用一个大根堆保存较小的一半数, 一个小根堆保存较大的一半数.
  2. 每次根据num和两个堆顶的数据决定往哪个堆里面放.
  3. 放完后进行平衡确保两个堆的size差不超过1.
  4. 利用两个堆的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)的更多相关文章

  1. lintcode 1: Data Stream Median

    Data Stream Median Numbers keep coming, return the median of numbers at every time a new number adde ...

  2. LintCode "Sliding Window Median" & "Data Stream Median"

    Besides heap, multiset<int> can also be used: class Solution { void removeOnly1(multiset<in ...

  3. 数据流中位数 · data stream median

    [抄题]: 数字是不断进入数组的,在每次添加一个新的数进入数组的同时返回当前新数组的中位数. [思维问题]: [一句话思路]: 左边x个元素,右边要有x+1个元素,因此利用maxheap把左边的最大值 ...

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

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

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

  7. Find Median from Data Stream

    常规方法 超时 class MedianFinder { vector<int> coll; public: MedianFinder(){ } void heapfu(vector< ...

  8. 数据结构与算法(1)支线任务8——Find Median from Data Stream

    题目如下:(https://leetcode.com/problems/find-median-from-data-stream/) Median is the middle value in an ...

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

随机推荐

  1. 再探Linux动态链接 -- 关于动态库的基础知识

      在近一段时间里,由于多次参与相关专业软件Linux运行环境建设,深感有必要将这些知识理一理,供往后参考. 编译时和运行时 纵观程序编译整个过程,细分可分为编译(Compiling,指的是语言到平台 ...

  2. sqlserver的增删改查

    select*from shuiguo  查询表 且小于等于7的范围) select distinct name from shuiguo--去除重复,只针对一列 select * from shui ...

  3. bzoj4637:期望

    思路:最小生成树计数只不过加了一个期望,由于期望具有线性性质,就可以转化为每条边的期望之和,那么一条边的期望如何求呢,在最小生成树记数中,是把相同边权的一起处理,之后把属于连通块内的点缩点,也就是说, ...

  4. wait(...) notify() notifyAll()

    简介 wait.notify.notifyAll是Java中3个与线程有关的方法,它们都是Object类中的方法. 其中,wait方法有3个重载形式: 1.wait() 2.wait(long tim ...

  5. OpenJudge/Poj 1661 帮助 Jimmy

    1.链接地址: bailian.openjudge.cn/practice/1661 http://poj.org/problem?id=1661 2.题目: 总Time Limit: 1000ms ...

  6. windows phone 生产含logo的二维码

    这几天了解二维码了解的比较多,不过就是没深入了解.google了一下生产含logo二维码的思路,就是把logo给画到生成的二维码上,还是因为二维码的纠错能力足够好啊,用Graphics对图片进行操作? ...

  7. async:false同步请求,浏览器假死

    // 异步请求导致数据错乱 // function get_num(){ // $("input[name='monitor']").eq(1).attr('checked',tr ...

  8. starling 中 的特效

    一. 最好用同一张图片进行缩放变形等处理后组合,这样可以每帧一draw

  9. NodeJS较高版本对connect支持的问题

    在nodejs中引入connect后,构建应用的代码如下 var connect = require('connect'); var server = connect.createServer(); ...

  10. CentOS 根据命令查所在的包

    在工作中经常会遇到想用某个命令机器没装却又不知道命令在哪个包(源码编译不再本文范围内),下面介绍个比较笨的方法可以帮助我们搞定这个问题. 说明:蓝色=命令名称       浅绿=命令参数       ...