数据结构与算法(1)支线任务8——Find Median from Data Stream
题目如下:(https://leetcode.com/problems/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 middle value. So the median is the mean of the two middle value.
Examples:
[2,3,4]
, the median is 3
[2,3]
, the median is (2 + 3) / 2 = 2.5
Design a data structure that supports the following two operations:
- void addNum(int num) - Add a integer number from the data stream to the data structure.
- double findMedian() - Return the median of all elements so far.
题目需要实现两个函数:向一个整数表中添加元素以及找中位数。以下是两个思路:
思路一
如果用vector储存整数,找中位数较容易(O(1)),添加整数可能会较耗时间,于是尝试使用时间复杂度为O(log(n))的二分插入。
class MedianFinder {
public:
bool odd; //长度是否为奇数。奇数为true,偶数为false
int begin, end, mid; //数组头、尾、中间的下标
vector<int> l; //数组 MedianFinder(): odd(false), begin(), end(), mid() {} //添加整数
void addNum(int num) {
//更新
odd = odd ? false : true;
begin = ;
end = l.size() - ;
//二分查找
while (begin <= end)
{
mid = (begin + end) / ;
if (l[mid] == num)
{
break;
}
else if (l[mid] > num)
{
end = mid - ;
}
else
{
begin = mid + ;
}
}
//插入
if (begin > end)
{
l.insert(l.begin()+begin, num);
}
else
{
l.insert(l.begin()+mid, num);
}
} //返回中位数
double findMedian() {
begin = ;
end = l.size() - ;
mid = (begin + end) / ;
if (odd)
{
return l[mid];
}
else
{
return ((double)l[mid] + l[mid+]) / ;
}
}
};
思路二
题目的提示说要用堆,于是考虑stl中的优先队列。可以将中位数两侧的数据分别储存至两个优先队列,一个整数大的优先级高(默认)(相当于排好序的大顶堆),另一个整数小的优先级高(相当于排好序的小顶堆)。
class MedianFinder {
public:
bool odd; //长度是否为奇数。奇数为true,偶数为false
priority_queue<int> front; //数组的前半部分,优先队列默认为较大的优先级高
priority_queue<int, vector<int>, greater<int>> back; //数组的后半部分,较小的数优先级高 MedianFinder(): odd(false) {} //添加整数
void addNum(int num) {
//更新长度的状态
odd = odd ? false : true;
//插入num,并保证front长度不小于back长度
if (odd)
{
if (back.size() && num > back.top())
{
back.push(num);
front.push(back.top());
back.pop();
}
else
{
front.push(num);
}
}
else
{
if (front.size() && num > front.top())
{
back.push(num);
}
else
{
front.push(num);
back.push(front.top());
front.pop();
}
}
} //返回中位数
double findMedian() {
if (odd)
{
return front.top();
}
else
{
return ((double)front.top() + back.top()) / ;
}
}
};
附:
//先留个坑,考完试再填吧……
数据结构与算法(1)支线任务8——Find Median from Data Stream的更多相关文章
- [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] 295. Find Median from Data Stream ☆☆☆☆☆(数据流中获取中位数)
295. Find Median from Data Stream&数据流中的中位数 295. Find Median from Data Stream https://leetcode.co ...
- 剑指offer 最小的k个数 、 leetcode 215. Kth Largest Element in an Array 、295. Find Median from Data Stream(剑指 数据流中位数)
注意multiset的一个bug: multiset带一个参数的erase函数原型有两种.一是传递一个元素值,如上面例子代码中,这时候删除的是集合中所有值等于输入值的元素,并且返回删除的元素个数:另外 ...
- [Swift]LeetCode295. 数据流的中位数 | 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 ...
- 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 ...
- 295. 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 ...
- Find Median from Data Stream 解答
Question Median is the middle value in an ordered integer list. If the size of the list is even, the ...
- LeetCode295-Find Median from Data Stream && 480. 滑动窗口中位数
中位数是有序列表中间的数.如果列表长度是偶数,中位数则是中间两个数的平均值. 例如, [2,3,4] 的中位数是 3 [2,3] 的中位数是 (2 + 3) / 2 = 2.5 设计一个支持以下两种操 ...
随机推荐
- java hook
linux下 hook的触发,需要 发送信号为15. 后续补充具体内容.
- Java数据类型、变量、运算符、语句。
数据类型:整型 int long short byte小数 double float 字符 char 转义字符:\'(单引号字符) \\(反斜杠字符) \n(换行) \r(回车) \t(水平制表符,相 ...
- python 实现文件下载
Requests库,高度封装的http库 import requests url = 'http://down.sandai.net/thunder9/Thunder9.0.18.448.exe' f ...
- [Note] Build your SDL2 Environment in Visual Studio 2013 配置你的SDL2运行环境
Right key your project in "solution manager(解决方案资源管理器)", choose the "Property(属性)&quo ...
- 【jQuery plug-in】DataTables
1. DOM Position dataTableOption.dom = '<"top"<"pull-left"l><"pu ...
- HDU-3247 Resource Archiver(AC自动机+BFS)
Description Great! Your new software is almost finished! The only thing left to do is archiving all ...
- HackerRank-Longest Subarray
give an array and target value, find the max length of the subarray which sum of the elements is les ...
- POJ做题笔记:1000,1004,1003
1000 A+B Problem 题目大意:输入两个数a和b,输出他们的和. 代码: #include <stdio.h> int main() { int a, b; while (sc ...
- kbengine里如何使用git快速下载项目?
项目有两个镜像,github[https://github.com/kbengine/kbengine.git] ,osc开源中国[https://git.oschina.net/likecg/kbe ...
- 基于 Annotation 拦截的 Spring AOP 权限验证方法
基于 Annotation 拦截的 Spring AOP 权限验证方法 转自:http://www.ibm.com/developerworks/cn/java/j-lo-springaopfilte ...