当前位置:网站首页>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); */
边栏推荐
- 音视频、编解码相关电子书、小工具,打包奉送!
- [English grammar] Unit1 articles, nouns, pronouns and numerals
- OpenCV视频质量诊断----视频遮挡诊断
- 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
- 记一次 .NET 差旅管理后台 CPU 爆高分析
- Learning notes [Gumbel softmax]
- PostgreSQL varchar[] array type operation
- 案例分享:QinQ基本组网配置
- Shell高级进阶
- ffmpeg 音频相关命令
猜你喜欢
DTD建模
ubuntu14安装MySQL并配置root账户本地与远程访问
为什么一定要从DevOps走向BizDevOps?
白盒加密技术浅理解
ECS summer money saving secret, this time @ old users come and take it away
任务:拒绝服务DoS
Introduction and installation of crunch, and making password dictionary with crunch
测试自学人必看:软件测试如何找测试项目?
Audio and video, encoding and decoding related e-books, gadgets, packaged for free!
[Mori city] random talk on GIS data (I)
随机推荐
Basic knowledge of audio coding and decoding
从零开始学 MySQL —数据库和数据表操作
703. 数据流中的第 K 大元素
[info() method in org.slf4j.logger]
Specification of lumiprobe reactive dye indocyanine green
EasyGBS网络不稳定情况下重复请求视频拉流问题的优化
【org.slf4j.Logger中info()方法】
sql查询去重统计的方法总结
XML syntax, constraints
【sql优化】with as 和 临时表的区别
【英语语法】Unit1 冠词、名词、代词和数词
H264 encoding profile & level control
EasyGBS主子码流都为H.265时,切换出现花屏如何解决?
Parallelism, concurrency and life cycle of threads
OpenCV视频质量诊断----视频遮挡诊断
IPv4地址、子网掩码、网关
118. 杨辉三角
奔赴山海之前,毕业季一定要做的那些事情
Solution of intelligent supply chain management platform in aquatic industry: support the digitalization of enterprise supply chain and improve enterprise management efficiency
使用环信提供的uni-app Demo,快速实现一对一单聊