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. mysql关键字讲解(join 、order by、group by、having、distinct)

    1.join     1.1 OUTER JOIN:想要包含右侧表中的所有行,以及左侧表中有匹配记录的行.        1.11 Mysql中有左连接(left join):            ...

  2. asp.net连接mysql数据库

    方法一:使用MySQL推出的MySQL Connector/Net组件, 该组件是MySQL为ADO.NET访问MySQL数据库设计的.NET专用访问组件.完成该组件后,需要在项目中引用这个组件,也可 ...

  3. oracle中的exists 和not exists 用法 in与exists语句的效率问题

    博文来源(oracle中的exists 和not exists 用法):http://chenshuai365-163-com.iteye.com/blog/1003247 博文来源(  in与exi ...

  4. ajaxError

    $(document).ready(function () { $('input:button').click(function() { if($('#fileName').val() == '') ...

  5. C# 数据类型映射 (SQLite,MySQL,MSSQL,Oracle)

    一.C# vs SQLite: C# SQLite 字段名 类型 库类型 GetFieldType(#) 转换 备注 F_BOOL bool BIT NOT NULL Boolean F_BOOL_N ...

  6. 学习C++ Primer 的个人理解(三)

    第三章,主要内容是字符串和数组.感觉作者的意图是希望读者可以早一点可以写出简单的小程序,并且可以早点接触迭代器这种思想. 在我看来,这种内容的难度并不大. 对于编程来说,最重要的应该是思想,类似vec ...

  7. 此一生 一个纯js的ajax

    /** * 得到ajax对象 */ function getajaxHttp() { var xmlHttp; try { // Firefox, Opera 8.0+, Safari xmlHttp ...

  8. java.util.regx Demo

    import java.util.regex.Matcher;import java.util.regex.Pattern; public class TestRegex { public stati ...

  9. osi七层模型和两主机传输过程:

    osi七层模型和两主机传输过程: http://www.zhihu.com/question/24002080/answer/31817536  注:笔记部分可能参考其他作者内容的一个记录,仅为加深自 ...

  10. leetcode Maximal Rectangle 单调栈

    作者:jostree 转载请注明出处 http://www.cnblogs.com/jostree/p/4052721.html 题目链接:leetcode Maximal Rectangle 单调栈 ...