当前位置:网站首页>Niuke.com: median in data flow
Niuke.com: median in data flow
2022-06-10 00:13:00 【lsgoose】


There are three ways to solve this problem
Catalog
1. violence
Read all the data directly and then use sort Sort the output median .
Here the type conversion uses C++11 Of static_cast<>
Because it is a fast platoon , So the time complexity is O(nlogn)
The code is as follows :
class Solution {
public:
#define SCD static_cast<double>
vector<int> v;
void Insert(int num) {
v.push_back(num);
}
double GetMedian() {
sort(v.begin(), v.end());
int sz=v.size();
if(sz & 1){// Odd numbers return the middle digit
return SCD(v[sz>>1]);
}else{
return SCD(v[sz>>1]+v[(sz>>1)-1])/2;
}
}
};2. Insertion sort
Here's one lower_bound Interface :
Its function is to return the first value in the array that is greater than or equal to the checked number
The method of finding the median is no different from that before .
The time complexity here is for each number O(logn) Of , The total time complexity is zero O(nlogn)
class Solution {
public:
#define SCD static_cast<double>
vector<int> v;
void Insert(int num) {
if(v.empty()){
v.push_back(num);
}else{
auto it=lower_bound(v.begin(), v.end(), num);
v.insert(it, num);
}
}
double GetMedian() {
int sz=v.size();
if(sz&1){
return SCD(v[sz>>1]);
}else{
return SCD(v[sz>>1]+v[(sz>>1)-1])/2;
}
}
};3. Pile up
Here's how to write the big and small top heap directly :
Small cap pile :
priority(int, vector<int>, less<int>)
Big pile top :
priority(int, vector<int>, greater<int>)
Our task is to maintain two top size heaps , It's understandable , We only need to use two numbers to separate the small and large parts , Why must two parts be ordered ? Based on this consideration , We use heap sort .
Pay attention to some details , The small part is stored in the big top pile , The large part is stored in the small top pile .
Ideas as follows :
Since it is the median requirement , We want the number of elements in the two heaps to be as close as possible , It can be divided into the following situations :
1. The number of elements in the large top heap is more than that in the small top heap , If we want to put new data in at this time , Obviously we should put it on the small top pile , But is it the new element we are adding now ? Obviously not , We should make use of the big top pile at this time , First, put the current element into the big top heap , Then take out the newly generated heap top and put it into the small top heap .
2. The rest is pretty much the same , It means that you put it in any heap .
At this time, the median is calculated according to the number of elements in the two heaps
The code is as follows :
class Solution {
public:
#define SCD static_cast<double>
priority_queue<int> min_q;// Small cap pile
priority_queue<int, vector<int>, greater<int>> max_q;// Big pile top
void Insert(int num) {
if(max_q.size()>=min_q.size()){
max_q.push(num);
min_q.push(max_q.top());
max_q.pop();
}else{
min_q.push(num);
max_q.push(min_q.top());
min_q.pop();
}
}
double GetMedian() {
if(max_q.size()==min_q.size()){
return SCD(max_q.top()+min_q.top())/2;
}else if(max_q.size()>min_q.size()){
return SCD(max_q.top());
}else{
return SCD(min_q.top());
}
}
};边栏推荐
- Install idea
- “当你不再是程序员,很多事会脱离掌控”—— 对话全球最大独立开源公司SUSE CTO
- MySQL development practice summary (I)
- 打破那面镜子,做回真正的自己(深度好文)
- C # WPF generates table with variable rows and columns from background code
- 732. my schedule III
- On chip variation (OCV) concept learning
- Is it necessary to get PMP?
- Is Hebei Hengyin futures a regular platform? Is it safe?
- AI chief architect 5-aica-wenxin NLP large model technology and Application
猜你喜欢

Record the 'new' course of an emergency investigation

SIGIR 2022 | 港大、武大提出KGCL:基于知识图谱对比学习的推荐系统

Book club recruits | let's read "Mr. toad goes to see a psychologist"

牛客网:数据流中的中位数

Manual single precision floating point type

数字大时代来临,360携手创业黑马助力中小企业抓住关键未来

MySQL开发实战总结(一)

ArcMap解决几何错误
流程测试支持批量参数导入,测试效率直接拉满!

"When you are no longer a programmer, many things will get out of control" -- dialogue with SUSE CTO, the world's largest independent open source company
随机推荐
Huangxiting: psychological research should follow the path of the Chinese people, and should do psychological research with Chinese characteristics
Do you know about singleton mode? I don't understand anyway.
ArcMap解决几何错误
SIGIR 2022 | 港大、武大提出KGCL:基于知识图谱对比学习的推荐系统
MySQL定时任务(Event Scheduler)
請問徽商期貨是正規的嗎?開戶交易安全嗎?
请问徽商期货是正规的吗?开户交易安全吗?
sparksql源码系列 | 一文搞懂Partitioning源码体系(spark3.2)
June 10, 2022 Daily: Huawei's top ten inventions were announced: efficient addition network and multi-objective game intelligent driving won the prize
C# WPF从后台代码生成行列可变的表格
(转)ArcGIS中ObjectID,FID和OID字段有什么区别?
ADB shell WM command using
478. randomly generate points in a circle
bias、variance介绍
929. unique email address
消息队列的精髓与灵魂
C#如何获取实体类属性名和值?
How to use the printer to print things
Do your filial duty to make an old people's fall prevention alarm system for your family
Spingboot+quartrz cluster version realizes dynamic timing tasks (using reflection to realize custom services)