数据结构与算法(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 设计一个支持以下两种操 ...
随机推荐
- 文本深度表示模型Word2Vec
简介 Word2vec 是 Google 在 2013 年年中开源的一款将词表征为实数值向量的高效工具, 其利用深度学习的思想,可以通过训练,把对文本内容的处理简化为 K 维向量空间中的向量运算,而向 ...
- 蓝牙-HCI错误码列表
错误码定义: /* Success code */ #define HCI_SUCCESS 0x00 /* Possible error codes */ #define HCI_UNKNOWN_HC ...
- mysql 导入导出方法。
1.导出 mysqldump -u username - p databasename >名.sql enter passward: 2.导入:mysql -uroot -proot sour ...
- xmind的第十三天笔记
- js中top、clientTop、scrollTop、offsetTop的区别 文字详细说明版【转】
之前在修改IE6下的一个显示bug时,查到过这些,贴这备忘,后面给出了详细说明,以前的版本,没仔细的说明,特希望大家也收藏下. 网页可见区域宽: document.body.clientWidth ...
- 接触到得到新语言里面涉及到很多关于ECMscript相关知识,所以还是来总结一下吧
ES6新增加了一些特性下面我就一边回忆一边写博文吧. 1.es6里面给我印象最深的应该就是箭头操作符(=>),它简化了函数的书写,第一次看见的时候完全懵逼,然后百度后才知道.操作符左边为输入的参 ...
- mySql中IFNULL的使用说明
IFNULL(expr1,expr2) 如果expr1不是NULL,IFNULL()返回expr1,否则它返回expr2.IFNULL()返回一个数字或字符串值 具体用法如:现有学生表(tbl_stu ...
- java的三大框架(一)
现在许许多多的初学者和程序员,都在趋之若鹜地学习Web开发的宝典级框架:Struts2,Spring,Hibernate.似乎这些框架成为了一个人是否精通Java,是否会写J2EE程序的唯一事实标准和 ...
- js数组键入值push和 arr[]i]区别
push 和 arr[i] 遍历 var arr = new Array(); $(":check").each(function(i){if(this.checked==true ...
- 总结.NET 中什么时候用 Static
静态类和类成员用于创建无需创建类的实例就能够访问的数据和函数.当类中没有依赖对象标识的数据或行为时,就可以使用静态类.静态类成员是可独立于任何对象标识的数据和行为,即无论对象发生什么更改,这些数据和函 ...