Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit ef987c0

Browse files
2 parents 2811c70 + 45a9987 commit ef987c0

File tree

2 files changed

+31
-0
lines changed

2 files changed

+31
-0
lines changed
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
all:
2+
gcc -O2 -o test kth_elem.c
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
#include <stdio.h>
2+
#include <stdlib.h>
3+
4+
static int compare(const void *a, const void *b)
5+
{
6+
return *(int *) a - *(int *) b;
7+
}
8+
9+
int findKthLargest(int* nums, int numsSize, int k) {
10+
qsort(nums, numsSize, sizeof(int), compare);
11+
return nums[numsSize - k];
12+
}
13+
14+
15+
int main(int argc, char **argv)
16+
{
17+
if (argc < 3) {
18+
fprintf(stderr, "Usage: ./test k n1 n2...\n");
19+
exit(-1);
20+
}
21+
22+
int i, count = argc - 2;
23+
int *nums = malloc(count * sizeof(int));
24+
for (i = 0; i < count; i++) {
25+
nums[i] = atoi(argv[i + 2]);
26+
}
27+
printf("%d\n", findKthLargest(nums, count, atoi(argv[1])));
28+
return 0;
29+
}

0 commit comments

Comments
 (0)