-
Notifications
You must be signed in to change notification settings - Fork 277
/
MaximumGap.java
55 lines (39 loc) · 1.42 KB
/
MaximumGap.java
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
class Solution {
// TC : O(n)
// SC : O(n)
public int maximumGap(int[] nums) {
int maxGap =0;
int n = nums.length;
// Base Case :
if(n < 2)
return 0;
int min = Integer.MAX_VALUE;
int max = Integer.MIN_VALUE;
for(int i : nums){
min = Math.min(min,i);
max = Math.max(max,i);
}
int bucketSize = (int)Math.ceil((double)(max-min)/(n -1));
int[] minOfBucket = new int[n -1];
int[] maxOfBucket = new int[n -1];
Arrays.fill(minOfBucket, Integer.MAX_VALUE);
Arrays.fill(maxOfBucket, Integer.MIN_VALUE);
//Set the min and max of bucket.
for(int i =0;i<n ; i++){
if(nums[i]== min || nums[i]==max)
continue;
int bucketIndexForCurrentEl = (nums[i] - min)/bucketSize;
minOfBucket[bucketIndexForCurrentEl] = Math.min(minOfBucket[bucketIndexForCurrentEl], nums[i]);
maxOfBucket[bucketIndexForCurrentEl] = Math.max(maxOfBucket[bucketIndexForCurrentEl], nums[i]);
}
// Calculate gap using MinB- MaxA
for(int i =0;i<n -1;i++){
if(maxOfBucket[i]==Integer.MIN_VALUE) // empty buckets
continue;
maxGap = Math.max(minOfBucket[i] - min, maxGap);
min = maxOfBucket[i];
}
maxGap = Math.max(maxGap, max-min);// 2
return maxGap;
}
}