-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathMaximumGap.java
More file actions
60 lines (52 loc) · 1.2 KB
/
MaximumGap.java
File metadata and controls
60 lines (52 loc) · 1.2 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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
/**
*
*/
package cc.dectinc.leetcode;
/**
* @author chenshijiang
* @date Apr 20, 2015 4:52:34 PM
*
*/
public class MaximumGap {
public int maximumGap(int[] num) {
int length = num.length;
if (length < 2) {
return 0;
}
int[] lowBounds = new int[length];
int[] upBounds = new int[length];
int min = Integer.MAX_VALUE, max = Integer.MIN_VALUE;
for (int a : num) {
min = Math.min(min, a);
max = Math.max(max, a);
}
int avgGap = (max - min + length) / length;
for (int a : num) {
int bucketIndex = (a - min) / avgGap;
int curMin = lowBounds[bucketIndex], curMax = upBounds[bucketIndex];
if (curMin == 0 || curMin > a) {
curMin = a;
}
if (curMax == 0 || curMax < a) {
curMax = a;
}
lowBounds[bucketIndex] = curMin;
upBounds[bucketIndex] = curMax;
}
int maxGap = upBounds[0] - lowBounds[0];
int preMax = upBounds[0];
for (int i = 1; i < length; i++) {
if (lowBounds[i] == 0) {
continue;
}
maxGap = Math.max(maxGap, lowBounds[i] - preMax);
preMax = upBounds[i];
}
return maxGap;
}
public static void main(String[] args) {
MaximumGap sol = new MaximumGap();
int[] num = {};
System.out.println(sol.maximumGap(num));
}
}