164. Maximum Gap

Source: Internet
Author: User
Tags integer division

    /** 164. Maximum Gap * 2016-6-4 by Mingyang * This topic first requires linear time, so my personal prediction is Bucketsort * Bucketsort is to divide a list into buckets and     Sort each bucket separately, and then together. * For example, I now have 10 if selection sort is 100 of the time complexity, then it needs to be divided into two 5 * Two 25, or 50 * Assuming n elements a to B. * Then the maximum difference will not be less than ceiling[(b-a)/(N-1)] * make bucket (bucket) Size len = ceiling[(b-a)/(N-1)], then there will be (B-A)/len + 1 Barrels *     For any integer K in an array, it is easy to find out where the bucket is by using 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. */     Public intMaximumgap (int[] num) {        if(num = =NULL|| Num.length < 2)            return0; //get the max and min value of the array        intMin = num[0]; intmax = Num[0];  for(inti:num) {min=math.min (min, i); Max=Math.max (max, i); }        //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        int[] Bucketsmax =New int[Num.length-1];//Store the max value in that bucketArrays.fill (Bucketsmin, Integer.max_value);        Arrays.fill (Bucketsmax, Integer.min_value); //put numbers into buckets         for(inti:num) {            if(i = = Min | | i = =max)Continue; intIDX = (i-min)/gap;//index of the right position in the bucketsBUCKETSMIN[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;  for(inti = 0; i < num.length-1; i++) {            if(Bucketsmin[i] = = Integer.max_value && Bucketsmax[i] = =integer.min_value)//Empty bucket                Continue; //min value minus the previous value is the current gapMaxgap = Math.max (Maxgap, Bucketsmin[i]-previous); //Update previous bucket valuePrevious =Bucketsmax[i]; } maxgap= Math.max (Maxgap, max-previous);//updata The final max value Gap        returnMaxgap; }

164. Maximum Gap

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.