forked from neetcode-gh/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
0347-top-k-frequent-elements.c
49 lines (42 loc) · 1.03 KB
/
0347-top-k-frequent-elements.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
// A structure to represent an element of the heap.
typedef struct {
int num;
int count;
} Heap;
static int compareHeap(const void* x, const void* y) {
return ((Heap*)y)->count - ((Heap*)x)->count;
}
/**
* Note: The returned array must be malloced, assume caller calls free().
*/
int* topKFrequent(int* nums, int numsSize, int k, int* returnSize){
int hash[20001] = {0};
Heap* heap;
int *res;
int i, j;
int c = 0;
for (i = 0; i < numsSize; i++) {
hash[nums[i]+10000]++;
if (hash[nums[i]+10000]) {
c++;
}
}
heap = (Heap*)malloc(sizeof(Heap) * c);
memset(heap, 0, sizeof(Heap) * c);
c = 0;
for (i = 0; i < 20001; i++) {
if(hash[i] > 0) {
heap[c].num = i - 10000;
heap[c].count = hash[i];
c++;
}
}
qsort(heap, c, sizeof(Heap), compareHeap);
c = 0;
res = (int*)malloc(sizeof(int) * k);
for (i = 0; i < k; i++) {
res[c++] = heap[i].num;
}
*returnSize = c;
return res;
}