[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 ...
随机推荐
- Linux下chkconfig命令详解即添加服务以及两种方式启动关闭系统服务
The command chkconfig is no longer available in Ubuntu.The equivalent command to chkconfig is update ...
- C. Sonya and Queries
http://codeforces.com/contest/714/problem/C 看到这题目,想想,肯定不能暴力啊,如果用map,如何快速找到满足要求的数目,然后,长度18,我想,这不是熟悉的t ...
- 学习C++ Primer 的个人理解(三)
第三章,主要内容是字符串和数组.感觉作者的意图是希望读者可以早一点可以写出简单的小程序,并且可以早点接触迭代器这种思想. 在我看来,这种内容的难度并不大. 对于编程来说,最重要的应该是思想,类似vec ...
- Poj 1006 / OpenJudge 2977 1006 Biorhythms/生理周期
1.链接地址: http://poj.org/problem?id=1006 http://bailian.openjudge.cn/practice/2977 2.题目: Biorhythms Ti ...
- Cassandra1.2文档学习(10)—— 插入和更新数据
参考数据:http://www.datastax.com/documentation/cassandra/1.2/webhelp/index.html#cassandra/dml/dml_about_ ...
- http通信协议详解
转载自:http://blog.csdn.net/gueter/article/details/1524447 引言 HTTP是一个属于应用层的面向对象的协议,由于其简捷.快速的方式,适用于分布式超媒 ...
- opengl多重采样
效果图如下,两幅图效果是一样的,只是换了个背景.两幅图均是左侧使用了多重采样,右侧的没有使用多重采样.
- Socket和SignalR
写到一半停电了,这心情真是哔了狗了,草稿箱竟然也没有!!! 好吧,这篇文档是之前写的记录,现在来完善(还是要完善的). 导读: 附件代码实现: Socket: 定义,同步实现,异步实现,还包括了TCP ...
- 大话string
最近看完大话string之后深有感悟,虽然写c#不知不觉的已经写了四年了,但是很多原理也一直不太明白,最近看完这个才算略微明白了一些. string类型有2个重要的特性,一致性和驻留性. 一致性就是说 ...
- 通过I2C总线向EEPROM中写入数据,记录开机次数
没买板子之前,用protues画过电路图,实现了通过i2c总线向EEPROM中写入和读出数据. 今天,在自己买的板子上面写关于i2c总线的程序,有个地方忘了延时,调程序的时候很蛋疼.下面说说我对I2c ...