数据结构与算法(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 设计一个支持以下两种操 ...
随机推荐
- How to ssh to your Amazon Elastic Beanstalk instance?
Well, if it's ec2 or a digital ocean server, it would be a lot easier- you do what you normally do f ...
- Django project structure: how does static folder, STATIC_URL, STATIC_ROOT work
So I've been messing up with Django(1.6+) project setting for quite sometime, this is what i finally ...
- Spring可以将简单的组件配置
这次听了老师的课程,觉得还是需要更加集中的去把各种题进行一个分类吧,然后有针对的去准备,虽然据说这一块在面试中也不容易考到,但是毕竟是难点,还是需要好好准备一下的.因为在dp这个方面,我算是一个比较新 ...
- Nutch插件原理
本文目的:讲解Nutch的插件运行时加载原理
- to_date & to_char
在oracle数据库中,有这么两个数据格式转换函数, to_date & to_char 这两个函数都可以将日期格式更改,但是一般都用前者,百度搜索中有个例子: select * from T ...
- 转:Android开发:使用JNI读取应用签名
博文转自http://www.tuicool.com/articles/UVjme2r,感谢博主的分享 为了防止被反编译,打算把关键代码写到so里(比如加解密),在so里加上判断APk包签名是否一致的 ...
- SqlServer阅读收集
1.根据字段名,查找相关表--INFORMATION_SCHEMA.COLUMNS SELECT * FROM INFORMATION_SCHEMA.COLUMNS WHERE COLUMN_NAME ...
- WCF初体验(C#操作Exchange)
最近再做一个Exchange的客户端维护工具,遇到了很多问题. 由于刚接触C#和Exchange,所以还需要继续学习.在此记录一下,只是一个新手的记录. 环境: 服务器:Exchang ...
- 跟我学Windows Azure 一 创建Windows Azure试用账号
我在网上看了很多教程,很大部分都是申请的是国外或者是香港的试用账号,而国内是由世纪互联所代理的,他的申请方式与VS2013的部署设置或多或少还是有些出入,这里我先跟大家一起过一下,在国内如何申请一个w ...
- zabbix问题处理
工作的时候回遇到各种各样的问题. 今天遇到一个关于zabbix的问题. "Zabbix agent on host.name is unreachable for 5 minutes&quo ...