当前位置:网站首页>Force buckle 295 Median data flow
Force buckle 295 Median data flow
2022-06-22 02:49:00 【SS_ zico】
The median is the number in the middle of the sequential table . If the list length is even , The median is the average of the two middle numbers .
for example ,
[2,3,4] The median of is 3
[2,3] The median of is (2 + 3) / 2 = 2.5
Design a data structure that supports the following two operations :
void addNum(int num) - Add an integer from the data stream to the data structure .
double findMedian() - Returns the median of all current elements .
Example :
addNum(1)
addNum(2)
findMedian() -> 1.5
addNum(3)
findMedian() -> 2
Method 1 : Insertion sort
class MedianFinder {
vector<int> store; // resize-able container
public:
// Adds a number into the data structure.
void addNum(int num)
{
if (store.empty())
store.push_back(num);
else
store.insert(lower_bound(store.begin(), store.end(), num), num); // binary search and insertion combined
}
// Returns the median of current data stream
double findMedian()
{
int n = store.size();
return n & 1 ? store[n / 2] : (store[n / 2 - 1] + store[n / 2]) * 0.5;
}
};
Time complexity :O(n)
Spatial complexity :O(n)
Method 2 : Two piles
class MedianFinder {
priority_queue<int> lo; // max heap
priority_queue<int, vector<int>, greater<int>> hi; // min heap
public:
// Adds a number into the data structure.
void addNum(int num)
{
lo.push(num); // Add to max heap
hi.push(lo.top()); // balancing step
lo.pop();
if (lo.size() < hi.size()) {
// maintain size property
lo.push(hi.top());
hi.pop();
}
}
// Returns the median of current data stream
double findMedian()
{
return lo.size() > hi.size() ? (double) lo.top() : (lo.top() + hi.top()) * 0.5;
}
};
/** * Your MedianFinder object will be instantiated and called as such: * MedianFinder* obj = new MedianFinder(); * obj->addNum(num); * double param_2 = obj->findMedian(); */
Time complexity :O(longn)
Spatial complexity :O(n)
边栏推荐
- ZCMU--1052: Holedox Eating(C语言)
- 最新發布:Neo4j 圖數據科學 GDS 2.0 和 AuraDS GA
- HarmonyOS鸿蒙使用ORM Bee访问数据库实例
- C mapster object mapper learning
- Vscode custom template, take notes with the template?!
- 自动化工具-监测文件的变化
- 【Percona-Toolkit】系列之pt-table-checksum和pt-table-sync 数据校验修复神器
- File upload vulnerability shooting range analysis upload_ LABS
- 微软 IE 浏览器于 6 月 15 日被永久关闭
- PMP pre exam guide on June 25, you need to do these well
猜你喜欢

PMP reference related agile knowledge

The neo4j skill tree was officially released to help you easily master the neo4j map database

Right and left vertical time axis with serial number

Pytorch visualization

xpm_ memory_ A complete example of using the tdpram primitive

The latest official product of domestic brand oppo! This ppt report! It really refreshes my understanding of it

Two dot vertical progress styles

Neo4j 智能供应链应用源代码简析

银联支付 返回商户 Nignx post请求405

EMC輻射發射整改-原理案例分析
随机推荐
最新发布:Neo4j 图数据科学 GDS 2.0 和 AuraDS GA
PMP reference related agile knowledge
C3-qt realize Gobang games (I) November 7, 2021
ModuleNotFoundError: No module named ‘torchvision. datasets‘; ‘ torchvision‘ is not a package
Use of day19qpushbutton 2021-10-30
【5. 高精度减法】
Day21qt mouse event 2021-11-01
[9. submatrix sum]
Relative references must start with either “/“, “./“, or “../“.
[Shangshui Shuo series] day two
[8. One dimensional prefix and]
EMC radiation emission rectification - principle case analysis
web框架概述与程序开发
C mapster object mapper learning
An article thoroughly learns to draw data flow diagrams
June25,2022 PMP Exam clearance manual-4
C1-qt idea of realizing simple calculator 2021.10.15
Openjudge noi 1.13 46: octal to decimal
【7. 高精度除法】
Live broadcast on June 22 | zhanzhihui, South China Institute of Technology: evolutionary computing for expensive optimization