标签:dia lin com lintcode queue 大根堆 空间复杂度 returns 利用
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大专栏 [OJ] Data Stream Median (Hard)n>());
}
}
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)
标签:dia lin com lintcode queue 大根堆 空间复杂度 returns 利用
原文地址:https://www.cnblogs.com/sanxiandoupi/p/11718028.html