当前位置:网站首页>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); */
边栏推荐
- 使用环信提供的uni-app Demo,快速实现一对一单聊
- What must be done in graduation season before going to Shanhai
- Witness the times! "The future of Renji collaboration has come" 2022 Hongji ecological partnership conference opens live broadcast reservation
- [go ~ 0 to 1] day 4 June 30 defer, structure, method
- Native JS creates a calendar - supports mouse wheel scrolling to select months - and can be ported to any framework
- Download (export) PDF template file (such as approval form), and report error: invalid nested tag * * * found, expected closing tag***
- GB28181的NAT穿透
- 实现一个Prometheus exporter
- optaplanner学习笔记(一)案例Cloud balance
- 【Go ~ 0到1 】 第四天 6月30 defer,结构体,方法
猜你喜欢
Chinese and English instructions human soluble advanced glycation end products receptor (sRAGE) ELISA Kit
ubuntu14安装MySQL并配置root账户本地与远程访问
云服务器ECS夏日省钱秘籍,这次@老用户快来领走
Solution of intelligent supply chain management platform in aquatic industry: support the digitalization of enterprise supply chain and improve enterprise management efficiency
原生js打造日程表-支持鼠标滚轮滚动选择月份-可以移植到任何框架中
[pytorch record] automatic hybrid accuracy training torch cuda. amp
ECS summer money saving secret, this time @ old users come and take it away
见证时代!“人玑协同 未来已来”2022弘玑生态伙伴大会开启直播预约
Introduction to relevant processes and functions of wechat official account development
论文阅读【Learning to Discretely Compose Reasoning Module Networks for Video Captioning】
随机推荐
【森城市】GIS数据漫谈(一)
Interview questions for audio and video positions in Dachang -- today's headline
Lean thinking: source, pillar, landing. I understand it after reading this article
直播HLS协议
axure不显示元件库
H264 encoding profile & level control
Bao, what if the O & M 100+ server is a headache? Use Xingyun housekeeper!
The use of subplot function in MATLAB
【无标题】
[pytorch record] automatic hybrid accuracy training torch cuda. amp
论文阅读【Discriminative Latent Semantic Graph for Video Captioning】
MATLAB中subplot函数的使用
论文阅读【Learning to Discretely Compose Reasoning Module Networks for Video Captioning】
OpenCV视频质量检测--清晰度检测
Solution of intelligent supply chain management platform in aquatic industry: support the digitalization of enterprise supply chain and improve enterprise management efficiency
Nacos configuration file publishing failed, please check whether the parameters are correct solution
ubuntu14安装MySQL并配置root账户本地与远程访问
Shell高级进阶
The intelligent epidemic prevention system provides safety guarantee for the resumption of work and production at the construction site
Ubuntu14 install MySQL and configure root account local and remote access