数据结构与算法(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 设计一个支持以下两种操 ...
随机推荐
- lua userdata
#define metatablename "studentlib.06-11-11" /** * utility functions */ static int pusherro ...
- 快速删除.svn文件夹
Windows Registry Editor Version 5.00 [HKEY_LOCAL_MACHINE\SOFTWARE\Classes\Folder\shell\DeleteSVN] @= ...
- C# 调用百度翻译Api
这是简单的界面.用的是wpf,winform也可以 具体的操作类 public partial class MainWindow : Window { string url = "" ...
- JS对象实现随机满天小星星实例
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...
- 【 D3.js 入门系列 --- 6 】 如何让图表动起来
[5.1]节中制作了一个比较完善的图表,但它是静态的,想做出它的动态效果吗?在D3中只需要短短的几行代码即可. 这一节将涉及4个函数的使用. 1.transition() 启动转变效果只需要添加这个即 ...
- 最短路径问题——floyd算法
floyd算法和之前讲的bellman算法.dijkstra算法最大的不同在于它所处理的终于不再是单源问题了,floyd可以解决任何点到点之间的最短路径问题,个人觉得floyd是最简单最好用的一种算法 ...
- hadoop 集群跑的时候用到hbasejar 文件的引用问题
1. 创建软连接 ln -s /home/hadoop/bigdater/hbase-0.98.6-cdh5.3.6/conf/hbase-site.xml ./hbase-site.xml(记得这里 ...
- hdu1260 dp
题意:有 k 个人需要买电影票,a[i] 表示第 i 个人单独买票要花费的时间,b[i] 表示第 i-1 个和第 i 个人一起买票需要花费的时间,问卖给所有人各一张票最少需要到什么时候. dp[i]表 ...
- android 观察者模式
1:观察者模式: 1:使用场景:一般使用在自定义控件的事件点击监听上面(或者封装方法进行回调) 2:写观察者模式步骤: (1):声明一个接口 (2):接口里面封装一个抽象方法 (3):需要封装一个 ...
- quick sort 的简化实现
Pivot 随机选取意义不大 第一种方法使用随机pivot,使得尽可能平均二分序列,而实际上一般来说需要排序的集合往往是乱序的,无需重新生成随机数作为pivot,大可使用固定位置的数作为pivot,这 ...