public int maximumGap(int[] nums) {
//using bucket sort
//min: minimum number of array
//max: maximum number of array
//THINK OF IT: since the max difference must not be smaller than ceiling[(max - min)/(N-1)]
//so we can construct N-1 bucket, the gap is ceiling[(max-min)/(N-1)]
//be aware of this case [1,1000]
//这个例子说明了max不一定能在bucket里面,可能刚好是边界
if (nums == null || nums.length < 2) return 0;
int n = nums.length;
//find global max and min of array
int min = Integer.MAX_VALUE;
int max = Integer.MIN_VALUE;
for(int num : nums){
min = Math.min(min, num);
max = Math.max(max, num);
}
//gao between two buckets
int gap = (int)Math.ceil((double)(max-min)/(n -1));
//construct two buckets to store the max and min of every bucket
int[] bucket_min = new int[n - 1];//bucket numer should be n - 1
int[] bucket_max = new int[n - 1];
//不能Arrays.fill(bucket_min, max); case: [3,6,9,1]
Arrays.fill(bucket_min, Integer.MAX_VALUE);
Arrays.fill(bucket_max, Integer.MIN_VALUE);
//find the max and min of every bucket
for(int num : nums){
if(num == max) continue;
int idx = (num - min)/gap;
bucket_min[idx] = Math.min(num, bucket_min[idx]);
bucket_max[idx] = Math.max(num, bucket_max[idx]);
}
int prev_max = min;
int maxGap = 0;
for(int i = 0; i < n - 1; i++){
//empty bucket
if(bucket_min[i] == Integer.MAX_VALUE && bucket_max[i] == Integer.MIN_VALUE){
continue;
}
//gap = min number of bucket[i] - max number of bucket[i-1]
maxGap = Math.max(maxGap,bucket_min[i] - prev_max);
prev_max = bucket_max[i];
}
//be aware of this case [1,1000]
//这个例子说明了max不一定能在bucket里面,可能刚好是边界
//所以需要再check 一次
maxGap = Math.max(maxGap, max - prev_max);
return maxGap;
}
public int maximumGap(int[] nums) {
if(nums == null || nums.length < 2) return 0;
int n = nums.length;
int[] sorted_nums = new int[n];
int bits = 10;//Integer.MAX_VALUE at most has 10 bits
//find the max number
int max = -1;
for(int num : nums){
max = Math.max(max, num);
}
int exp = 1;
while(max / exp > 0){
int[] counts = new int[10];//total count of num i
//数0,1,2,3,4~9的数分别出现的次数
for(int i = 0; i < n; i++){
counts[nums[i]/exp % 10]++;
}
//increment the total count
for(int i = 1; i < 10; i++){
counts[i] += counts[i-1];
}
//从后往前根据counts[]把nums[i]放在相应的位置,注意先--counts[]
for(int i = n - 1; i >= 0; i--){
sorted_nums[--counts[nums[i]/exp%10]] = nums[i];
}
//assign sorted_nums to nums
for(int i = 0; i< n; i++){
nums[i] = sorted_nums[i];
}
exp *= 10;
}
int maxGap = 0;
//now the nums has been sorted
for(int i = 1; i < n; i++){
maxGap = Math.max(maxGap, nums[i] - nums[i-1]);
}
return maxGap;
}
}