-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMaximum Gap.java
More file actions
41 lines (37 loc) · 1.12 KB
/
Maximum Gap.java
File metadata and controls
41 lines (37 loc) · 1.12 KB
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
public class Solution {
public int maximumGap(int[] num) {
if (num == null || num.length < 2) return 0;
int min = num[0];
int max = num[0];
for (int i:num) {
if (max < i){
max = i;
}
else if (min > i){
min = i;
}
}
int average = (int)Math.ceil((double)(max - min)/(num.length - 1));
int[] mins = new int[num.length - 1];
int[] maxs = new int[num.length - 1];
Arrays.fill(mins, Integer.MAX_VALUE);
Arrays.fill(maxs, Integer.MIN_VALUE);
for (int i:num) {
if (i == min || i == max)
continue;
int index = (i - min) / average;
mins[index] = Math.min(i, mins[index]);
maxs[index] = Math.max(i, maxs[index]);
}
int ret = Integer.MIN_VALUE;
int previous = min;
for (int i = 0; i < num.length - 1; i++) {
if (mins[i] == Integer.MAX_VALUE && maxs[i] == Integer.MIN_VALUE)
continue;
ret = Math.max(ret, mins[i] - previous);
previous = maxs[i];
}
ret = Math.max(ret, max - previous);
return ret;
}
}