当前位置:网站首页>703. The k-th element in the data flow
703. The k-th element in the data flow
2022-07-01 19:35:00 【Mr Gao】
703. The... In the data stream K Big element
Design a data stream to find the k Classes of large elements (class). Note that it's the... After the order k Big element , Not the first k A different element .
Please implement KthLargest class :
KthLargest(int k, int[] nums) Use integers k And integer stream nums Initialize object .
int add(int val) take val Insert data stream nums after , Returns the current data stream at k Big elements .
Example :
Input :
[“KthLargest”, “add”, “add”, “add”, “add”, “add”]
[[3, [4, 5, 8, 2]], [3], [5], [10], [9], [4]]
Output :
[null, 4, 5, 5, 8, 8]
explain :
KthLargest kthLargest = new KthLargest(3, [4, 5, 8, 2]);
kthLargest.add(3); // return 4
kthLargest.add(5); // return 5
kthLargest.add(10); // return 5
kthLargest.add(9); // return 8
kthLargest.add(4); // return 8
typedef struct {
struct KthLargest *next;
int val
} KthLargest;
KthLargest* kthLargestCreate(int k, int* nums, int numsSize) {
KthLargest *l=(KthLargest*)malloc(sizeof(KthLargest));
l->val=k;
l->next=NULL;
int i;
for(i=0;i<numsSize;i++){
KthLargest *p=(KthLargest*)malloc(sizeof(KthLargest));
p->val=nums[i];
KthLargest *pre=l,*s=l->next;
if(i==0){
p->next=l->next;
l->next=p;
}
else{
while(s){
if(s->val>=nums[i]){
break;}
pre=s;
s=s->next;
}
p->next=s;
pre->next=p;
}
}
return l;
}
int kthLargestAdd(KthLargest* obj, int val) {
KthLargest *pre=obj,*s=obj->next;
KthLargest *p=(KthLargest*)malloc(sizeof(KthLargest));
p->val=val;
while(s){
if(s->val>=val){
break;}
pre=s;
s=s->next;
}
p->next=s;
pre->next=p;
int i=1;
s=obj->next;
while(i<=obj->val-2){
s=s->next;
i++;
}
KthLargest *t=s->next;
s->next=t->next;
s=obj->next;
while(s){
printf("%d ",s->val);
s=s->next;
}
printf("|");
return t->val;
}
void kthLargestFree(KthLargest* obj) {
}
/** * Your KthLargest struct will be instantiated and called as such: * KthLargest* obj = kthLargestCreate(k, nums, numsSize); * int param_1 = kthLargestAdd(obj, val); * kthLargestFree(obj); */
边栏推荐
猜你喜欢

宝,运维100+服务器很头疼怎么办?用行云管家!

Bao, what if the O & M 100+ server is a headache? Use Xingyun housekeeper!

Specification of lumiprobe reactive dye indocyanine green

【pytorch记录】自动混合精度训练 torch.cuda.amp

pickle.load报错【AttributeError: Can‘t get attribute ‘Vocabulary‘ on <module ‘__main__‘】

Dlib+opencv library for fatigue detection

Les canaux de culture intensive s'efforcent de développer Fu Xin et Wei Shi jiajie pour organiser une conférence de formation sur les nouveaux produits

商业智能BI开发和报表开发有什么本质区别?

【To .NET】C#集合类源码解析

Learn MySQL from scratch - database and data table operations
随机推荐
Enabling "new Chinese enterprises", SAP process automation landing in China
How to solve the problem of splash screen when the main and sub code streams of easygbs are h.265?
Dlib+opencv library for fatigue detection
uni-app微信小程序一键登录获取权限功能
研究了11种实时聊天软件,我发现都具备这些功能…
【Go ~ 0到1 】 第四天 6月30 defer,结构体,方法
GB28181的NAT穿透
【英语语法】Unit1 冠词、名词、代词和数词
EasyGBS网络不稳定情况下重复请求视频拉流问题的优化
Chinese and English instructions human soluble advanced glycation end products receptor (sRAGE) ELISA Kit
win10下使用msys+vs2019编译ffmpeg源码
XML syntax, constraints
English语法_形容词/副词3级 -注意事项
Parallelism, concurrency and life cycle of threads
精耕渠道共謀發展 福昕攜手偉仕佳傑開展新產品培訓大會
【6.24-7.1】写作社区精彩技术博文回顾
A brief understanding of white box encryption technology
The use of subplot function in MATLAB
商业智能BI开发和报表开发有什么本质区别?
奔赴山海之前,毕业季一定要做的那些事情