-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
0703-kth-largest-element-in-a-stream.c
145 lines (121 loc) · 3.37 KB
/
0703-kth-largest-element-in-a-stream.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
struct heap {
int* array;
int len;
int max_len;
bool (*append)(struct heap* self, int val);
int (*pop)(struct heap* self);
};
bool heap_insert(struct heap*, int);
int heap_pop(struct heap*);
struct heap* Heap(int max_len) {
int* array = (int *)malloc(max_len * sizeof(int));
struct heap* heap = (struct heap*)malloc(sizeof(struct heap));
heap -> array = array;
heap -> max_len = max_len;
heap -> len = 0;
heap -> append = heap_insert;
heap -> pop = heap_pop;
return heap;
}
int heap_parent(int index) {
return (index - 1) / 2;
}
int heap_left(int index) {
return 2 * index + 1;
}
int heap_right(int index) {
return 2 * index + 2;
}
void heap_swap(struct heap* heap, int parent, int index) {
int temp = heap -> array[index];
heap -> array[index] = heap -> array[parent];
heap -> array[parent] = temp;
}
void heap_heapify(struct heap* heap, int index) {
if (index != 0) {
int parent = heap_parent(index);
if (heap -> array[parent] > heap -> array[index]) {
heap_swap(heap, parent, index);
heap_heapify(heap, parent);
}
}
}
bool heap_insert(struct heap* heap, int val) {
if (heap -> len == heap -> max_len) {
return false;
}
heap -> array[heap -> len] = val;
heap_heapify(heap, heap -> len);
heap -> len += 1;
return true;
}
bool heap_has_left(struct heap* heap,int index) {
return heap_left(index) < heap -> len;
}
bool heap_has_right(struct heap* heap, int index) {
return heap_right(index) < heap -> len;
}
void heap_heapify_reverse(struct heap* heap, int index) {
int left = heap_left(index);
int right = heap_right(index);
int minimum = index;
if (heap_has_left(heap, index) && (heap -> array[left] < heap -> array[minimum])) {
minimum = left;
}
if (heap_has_right(heap, index) && (heap -> array[right] < heap -> array[minimum])) {
minimum = right;
}
if (minimum != index) {
heap_swap(heap, index, minimum);
heap_heapify_reverse(heap, minimum);
}
}
int heap_pop(struct heap* heap) {
if (heap -> len == 0) {
return NULL;
}
int val = heap -> array[0];
heap -> len -= 1;
heap -> array[0] = heap -> array[heap -> len];
if (heap -> len > 1) {
heap_heapify_reverse(heap, 0);
}
return val;
}
void heap_free(struct heap* heap) {
free(heap -> array);
free(heap);
}
typedef struct {
struct heap* heap;
int k;
} KthLargest;
KthLargest* kthLargestCreate(int k, int* nums, int numsSize) {
KthLargest* self = malloc(sizeof(KthLargest));
self -> heap = Heap(numsSize + 2);
self -> k = k;
for (int i = 0; i < numsSize; i++) {
self -> heap -> append(self -> heap, nums[i]);
}
while (self -> heap -> len > k) {
self -> heap -> pop(self -> heap);
}
return self;
}
int kthLargestAdd(KthLargest* obj, int val) {
obj -> heap -> append(obj -> heap, val);
if (obj -> heap -> len > obj -> k) {
obj -> heap -> pop(obj -> heap);
}
return obj -> heap -> array[0];
}
void kthLargestFree(KthLargest* obj) {
heap_free(obj -> heap);
free(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);
*/