leetcode.295. Find Median from Data Stream
2016-05-24 22:38
501 查看
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:
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.
For example:
class MedianFinder {
private:
priority_queue<int,vector<int>, greater<int>> minHeap;
priority_queue<int,vector<int>, less<int>> maxHeap;
public:
// Adds a number into the data structure.
void addNum(int num) {
if (maxHeap.size() == minHeap.size())
{
if (minHeap.size() != 0 && num > minHeap.top())
{
maxHeap.push(minHeap.top());
minHeap.pop();
minHeap.push(num);
}
else
{
maxHeap.push(num);
}
}
else
{
if (num < maxHeap.top())
{
minHeap.push(maxHeap.top());
maxHeap.pop();
maxHeap.push(num);
}
else
{
minHeap.push(num);
}
}
}
// Returns the median of current data stream
double findMedian() {
if (maxHeap.size() == 0)
return 0;
if (maxHeap.size() == minHeap.size())
return ((double)minHeap.top()+(double)maxHeap.top())/2;
else
return (double)maxHeap.top();
}
};
// Your MedianFinder object will be instantiated and called as such:
// MedianFinder mf;
// mf.addNum(1);
// mf.findMedian();
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.
For example:
add(1) add(2) findMedian() -> 1.5 add(3) findMedian() -> 2
class MedianFinder {
private:
priority_queue<int,vector<int>, greater<int>> minHeap;
priority_queue<int,vector<int>, less<int>> maxHeap;
public:
// Adds a number into the data structure.
void addNum(int num) {
if (maxHeap.size() == minHeap.size())
{
if (minHeap.size() != 0 && num > minHeap.top())
{
maxHeap.push(minHeap.top());
minHeap.pop();
minHeap.push(num);
}
else
{
maxHeap.push(num);
}
}
else
{
if (num < maxHeap.top())
{
minHeap.push(maxHeap.top());
maxHeap.pop();
maxHeap.push(num);
}
else
{
minHeap.push(num);
}
}
}
// Returns the median of current data stream
double findMedian() {
if (maxHeap.size() == 0)
return 0;
if (maxHeap.size() == minHeap.size())
return ((double)minHeap.top()+(double)maxHeap.top())/2;
else
return (double)maxHeap.top();
}
};
// Your MedianFinder object will be instantiated and called as such:
// MedianFinder mf;
// mf.addNum(1);
// mf.findMedian();
相关文章推荐
- Echarts 使用总结
- leetcode---Roman to Integer
- c++作业6
- LinkedList集合源码解析
- TLV基础
- 飛飛(四十六)交通工具
- 数组中只出现一次的数字
- maven之window安装
- 【c++程序】friend与operator
- 4Sum
- 个人冲刺——第二天
- 面经中高频知识点归纳(四)
- Spark中组件Mllib的学习29之支持向量机SVM-方法2
- 【软考】这一个五月,你陪我一起走过
- 尝试解决JPA懒加载异常问题(wildfly)
- MATLAB与VC混编中的DLL加载问题
- Android UI设计之RecyclerView
- PID自学笔记-入门级
- org.apache.ibatis.builder.IncompleteElementException : Could not find parameter map com.vialove.ais
- iOS开发小技巧--根据文字,计算label中文字高度