当前位置:网站首页>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); */
边栏推荐
- Learning notes [Gumbel softmax]
- 【Go ~ 0到1 】 第四天 6月30 defer,结构体,方法
- Facebook聊单,SaleSmartly有妙招!
- Thesis reading [distinctive late semantic graph for video capturing]
- Dlib+opencv library for fatigue detection
- 数字化转型企业成功的关键,用数据创造价值
- 事务隔离级别 gap锁 死锁
- ffmpeg常用命令(二)
- 【森城市】GIS数据漫谈(一)
- Learning notes - steps of JDBC connection database operation
猜你喜欢

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

Shell高级进阶

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

Crunch简介、安装,使用Crunch制作密码字典
![pickle. Load error [attributeerror: can't get attribute 'volatile' on < module '\u main']](/img/98/c4df0747856eda262b82942eedad8f.png)
pickle. Load error [attributeerror: can't get attribute 'volatile' on < module '\u main']

新版国标GB28181视频平台EasyGBS如何配置WebRTC视频流格式播放?

nacos启动失败问题解决与总结

Dom4j parsing XML, XPath retrieving XML

Witness the times! "The future of Renji collaboration has come" 2022 Hongji ecological partnership conference opens live broadcast reservation

Introduction and installation of crunch, and making password dictionary with crunch
随机推荐
Shell高级进阶
Helium transmission line of lake shore cryostat
sql查询去重统计的方法总结
win10下使用msys+vs2019编译ffmpeg源码
Learning notes [Gumbel softmax]
[pytorch record] automatic hybrid accuracy training torch cuda. amp
学习笔记-JDBC连接数据库操作的步骤
简版拼多多商品数据
pickle.load报错【AttributeError: Can‘t get attribute ‘Vocabulary‘ on <module ‘__main__‘】
大厂音视频职位面试题目--今日头条
微信公众号开发相关流程及功能介绍
【org.slf4j.Logger中info()方法】
[pytorch record] distributed training dataparallel and distributeddataparallel of the model
数字化转型企业成功的关键,用数据创造价值
MySQl的基本使用
English grammar_ Adjective / adverb Level 3 - precautions
Lumiprobe free radical analysis h2dcfda instructions
Solidity - truncated and checked modes of arithmetic operations - new features of 0.8.0
Audio and video, encoding and decoding related e-books, gadgets, packaged for free!
Nat penetration of gb28181