Given an unsorted array, find the maximum difference between the successive elements in its sorted form. Try to solve it in lineartime/If the array contains less than 2 elements. Assume all elementsin the array is non-negative integers and fit in the 32-bit signed integer range.
Official version (bucket sort):
Suppose there are n elements a through B.
Then the maximum difference is not less than ceiling[(b-a)/(N-1)]
Make bucket (bucket) Size len = ceiling[(b-a)/(N-1)]
For any integer K in an array, it is easy to find the position of the bucket by using the equation loc = (k-a)/Len, and then maintain the maximum and minimum values for each bucket
Because the difference between elements in the same bucket is at most len-1, the final answer will not be selected from the same bucket.
For each non-empty bucket p, find the next non-empty bucket Q, then Q.min-p.max may be an alternative answer. Returns the maximum value in all of these possible values.
Here A is min,b is Max, the bucket has num.length-1. MIN, Max does not participate in the bucket, except Min and Max there are N-2 numbers and N-1 barrels, so there must be an empty bucket. Because of the existence of an empty bucket, a previous variable is used to represent Max for the last non-empty bucket. Previous is initialized to Min, so min takes into account although min is not in the bucket. Also remember to think about Max, so after you finally walk through the bucket, you have to be more than Max
The algorithm o (n) Time and space
Also pay attention to Math.floor, can not use, to use math.ceil such as: 2/2.667 = 0.7499062617172854, the expectation is: 1, but floor will give 0,ceil to 1.
1 Public classSolution {2 Public intMaximumgap (int[] num) {3 if(num = =NULL|| Num.length < 2)4 return0;5 //get the max and min value of the array6 intMin = num[0];7 intmax = Num[0];8 for(inti:num) {9Min =math.min (min, i);TenMax =Math.max (max, i); One } A //the minimum possibale gap, ceiling of the integer division - intGap = (int) Math.ceil (Double) (Max-min)/(num.length-1)); - int[] Bucketsmin =New int[Num.length-1];//Store the Min value in that bucket the int[] Bucketsmax =New int[Num.length-1];//Store the max value in that bucket - Arrays.fill (Bucketsmin, integer.max_value); - Arrays.fill (Bucketsmax, integer.min_value); - //put numbers into buckets + for(inti:num) { - if(i = = Min | | i = =max) + Continue; A intIDX = (i-min)/gap;//index of the right position in the buckets atBUCKETSMIN[IDX] =math.min (i, Bucketsmin[idx]); -BUCKETSMAX[IDX] =Math.max (i, Bucketsmax[idx]); - } - //Scan the buckets for the Max Gap - intMaxgap =Integer.min_value; - intPrevious =min; in for(inti = 0; i < num.length-1; i++) { - if(Bucketsmin[i] = = Integer.max_value && Bucketsmax[i] = =integer.min_value) to //Empty bucket + Continue; - //min value minus the previous value is the current gap theMaxgap = Math.max (Maxgap, Bucketsmin[i]-previous); * //Update previous bucket value $Previous =Bucketsmax[i];Panax Notoginseng } -Maxgap = Math.max (Maxgap, max-previous);//updata The final max value Gap the returnMaxgap; + } A}
Leetcode:maximum Gap