Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit9f98b00

Browse files
committed
Add C solution for 703-Kth-Largest-Element-In-A-Stream
1 parentea6419c commit9f98b00

File tree

1 file changed

+145
-0
lines changed

1 file changed

+145
-0
lines changed
Lines changed: 145 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,145 @@
1+
2+
structheap {
3+
int*array;
4+
intlen;
5+
intmax_len;
6+
bool (*append)(structheap*self,intval);
7+
int (*pop)(structheap*self);
8+
};
9+
10+
boolheap_insert(structheap*,int);
11+
intheap_pop(structheap*);
12+
13+
structheap*Heap(intmax_len) {
14+
int*array= (int*)malloc(max_len*sizeof(int));
15+
structheap*heap= (structheap*)malloc(sizeof(structheap));
16+
heap->array=array;
17+
heap->max_len=max_len;
18+
heap->len=0;
19+
heap->append=heap_insert;
20+
heap->pop=heap_pop;
21+
returnheap;
22+
}
23+
24+
intheap_parent(intindex) {
25+
return (index-1) /2;
26+
}
27+
28+
intheap_left(intindex) {
29+
return2*index+1;
30+
}
31+
32+
intheap_right(intindex) {
33+
return2*index+2;
34+
}
35+
36+
voidheap_swap(structheap*heap,intparent,intindex) {
37+
inttemp=heap->array[index];
38+
heap->array[index]=heap->array[parent];
39+
heap->array[parent]=temp;
40+
}
41+
42+
voidheap_heapify(structheap*heap,intindex) {
43+
if (index!=0) {
44+
intparent=heap_parent(index);
45+
if (heap->array[parent]>heap->array[index]) {
46+
heap_swap(heap,parent,index);
47+
heap_heapify(heap,parent);
48+
}
49+
}
50+
}
51+
52+
boolheap_insert(structheap*heap,intval) {
53+
54+
if (heap->len==heap->max_len) {
55+
return false;
56+
}
57+
heap->array[heap->len]=val;
58+
heap_heapify(heap,heap->len);
59+
heap->len+=1;
60+
return true;
61+
}
62+
63+
boolheap_has_left(structheap*heap,intindex) {
64+
returnheap_left(index)<heap->len;
65+
}
66+
67+
boolheap_has_right(structheap*heap,intindex) {
68+
returnheap_right(index)<heap->len;
69+
}
70+
71+
voidheap_heapify_reverse(structheap*heap,intindex) {
72+
73+
intleft=heap_left(index);
74+
intright=heap_right(index);
75+
intminimum=index;
76+
if (heap_has_left(heap,index)&& (heap->array[left]<heap->array[minimum])) {
77+
minimum=left;
78+
}
79+
if (heap_has_right(heap,index)&& (heap->array[right]<heap->array[minimum])) {
80+
minimum=right;
81+
}
82+
if (minimum!=index) {
83+
heap_swap(heap,index,minimum);
84+
heap_heapify_reverse(heap,minimum);
85+
}
86+
}
87+
88+
intheap_pop(structheap*heap) {
89+
if (heap->len==0) {
90+
returnNULL;
91+
}
92+
intval=heap->array[0];
93+
heap->len-=1;
94+
heap->array[0]=heap->array[heap->len];
95+
if (heap->len>1) {
96+
heap_heapify_reverse(heap,0);
97+
}
98+
returnval;
99+
}
100+
101+
voidheap_free(structheap*heap) {
102+
free(heap->array);
103+
free(heap);
104+
}
105+
106+
typedefstruct {
107+
structheap*heap;
108+
intk;
109+
}KthLargest;
110+
111+
112+
KthLargest*kthLargestCreate(intk,int*nums,intnumsSize) {
113+
114+
KthLargest*self=malloc(sizeof(KthLargest));
115+
self->heap=Heap(numsSize+2);
116+
self->k=k;
117+
for (inti=0;i<numsSize;i++) {
118+
self->heap->append(self->heap,nums[i]);
119+
}
120+
while (self->heap->len>k) {
121+
self->heap->pop(self->heap);
122+
}
123+
returnself;
124+
}
125+
126+
intkthLargestAdd(KthLargest*obj,intval) {
127+
obj->heap->append(obj->heap,val);
128+
if (obj->heap->len>obj->k) {
129+
obj->heap->pop(obj->heap);
130+
}
131+
returnobj->heap->array[0];
132+
}
133+
134+
voidkthLargestFree(KthLargest*obj) {
135+
heap_free(obj->heap);
136+
free(obj);
137+
}
138+
139+
/**
140+
* Your KthLargest struct will be instantiated and called as such:
141+
* KthLargest* obj = kthLargestCreate(k, nums, numsSize);
142+
* int param_1 = kthLargestAdd(obj, val);
143+
144+
* kthLargestFree(obj);
145+
*/

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp