forked from lennylxx/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
164.c
58 lines (45 loc) · 1.3 KB
/
164.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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int maximumGap(int num[], int n) {
if (n < 2) return 0;
/* radix sort */
int *sorted_num = (int *)calloc(n, sizeof(int));
int *temp = (int *)calloc(n, sizeof(int));
memcpy(sorted_num, num, sizeof(int) * n);
int i, j;
int shift;
for (shift = 0; shift < 32; shift += 8) {
int count[0x100] = {};
for (i = 0; i < n; i++) {
count[(sorted_num[i] >> shift) & 0xFF] ++;
}
int idx = 0, t = 0;
for (j = 0; j < 0x100; j++) {
t = count[j];
count[j] = idx;
idx += t;
}
for (i = 0; i < n; i++) {
temp[count[(sorted_num[i] >> shift) & 0xFF] ++] = sorted_num[i];
}
int *p = sorted_num;
sorted_num = temp;
temp = p;
}
int max_diff = 0;
for (i = 1; i < n; i++) {
if (sorted_num[i] - sorted_num[i - 1] > max_diff) {
max_diff = sorted_num[i] - sorted_num[i - 1];
}
}
free(sorted_num);
free(temp);
return max_diff;
}
int main() {
int A[] = { 4, 5, 3, 2, 9, 12, 32, 5 };
/* should be 20 = abs(12 - 32) */
printf("%d\n", maximumGap(A, sizeof(A)/sizeof(A[0])));
return 0;
}