-
Notifications
You must be signed in to change notification settings - Fork 0
/
hIndex.c
44 lines (40 loc) · 939 Bytes
/
hIndex.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
#include <stdio.h>
#include <stdlib.h>
/*
Testcase:
[3,0,6,1,5]
[1,3,1]
[4,4,4,4,4,4,5]
[3, 1, 7, 8, 9]
[3,4]
[0,1]
[1,2]
[5]
[0]
[2,3,3,3]
[4,4,0,0] - Expect hIndex = 2
[1,3,3] - Expect hIndex = 2
*/
int compare (const void* a, const void* b) {
return *(int*)a - *(int*)b;
}
int hIndex(int* citations, int citationsSize) {
qsort(citations, citationsSize, sizeof(int), compare);
int h_idx = citationsSize;
int remainings;
for (int i = 0; i < citationsSize; i++) {
remainings = citationsSize - i;
if (citations[i] <= remainings) {
h_idx = citations[i];
} else {
h_idx = (remainings > h_idx) ? remainings : h_idx;
}
}
return h_idx;
}
int main() {
int citationsSize = 5;
int citations[] = {3,0,6,1,5};
int res = hIndex(citations, citationsSize);
printf("H-index = %d \n", res);
}