Lintcode: Maximum interval

Source: Internet
Author: User
Tags lintcode

Topic

Given an unordered array, find the maximum spacing for two consecutive features in its sorted table.

If there are fewer than 2 features in the array, return 0.

Precautions

You can assume that all features in an array are non-negative integers and do not exceed a maximum of 32-bit integers.

Sample Example

Given an array [1, 9, 2, 5] , the sort table is [1, 2, 5, 9] , its maximum spacing is 5 between and 9 , = 4 .

Solving

Find the maximum interval after sorting

Find adjacent maximum after heap sort

classSolution {/**     * @paramnums:an array of integers *@return: The maximum difference*/     Public intMaximumgap (int[] nums) {        //Write your code hereHeapsort (nums); intres = 0;  for(inti=0;i<nums.length-1;i++) {res= Math.max (Res,nums[i+1]-Nums[i]); }        returnRes; }         Public intLeftinti) {        intL = i*2+1; returnl; }     Public intRightinti) {        intr = i*2 +2; returnR; }     Public voidHeapsort (int[] A) {        intn= a.length-1;                Maxheap (A,n);  for(inti=n;i>=1;i--) {swap (A,0, i); Inserttoheap (A,0,i-1); }    }    //building a large top heap     Public voidMaxheap (int[] A,intN) {                 for(inti=n/2;i>=0;i--) {inserttoheap (a,i,n); }    }    //position I inserted in the heap, adjusted to a large top heap     Public voidInserttoheap (int[] A,intIintN) {        intL =-1; intr =-1; intlargest =-1;  while(true) {L=Left (i); R=Right (i); //find the maximum value ID            if(L<=n && A[l] >A[i]) {Largest=l; }Else{Largest=i; }            if(R<=n && A[r] >A[largest]) {Largest=R; }            if(largest!=i) {//unequal time exchange, and update I loop to doswap (a,largest,i); I=largest; }Else{                 Break; }                    }    }     Public voidSwapint[] A,intIintj) {        intTMP =A[i]; A[i]=A[j]; A[J]=tmp; }}

Sorting by buckets

Reference links

with n Buckets

Put n data into a bucket

The maximum and minimum value of the data stored in the bucket that can be placed in the bucket

The edge of the bucket defines Low,high, which represents the boundary of the data that is placed in the bucket, the size of n buckets d = high-low is fixed, and d = (max-min)/n

So there's no data in one bucket.

The maximum interval must be between the adjacent barrels, Gap = Now.low-pre.high

classSolution {/**     * @paramnums:an array of integers *@return: The maximum difference*/      classbucket{intLow ; intHigh ;  PublicBucket () { low=-1; High=-1; }}  Public intMaximumgap (int[] num) {    if(num = =NULL|| Num.length < 2){        return0; }    //Find maximum minimum value    intmax = Num[0]; intMin = num[0];  for(intI=1; i<num.length; i++) {Max=Math.max (Max, num[i]); Min=math.min (min, num[i]); }     //initialize an array of bucketsbucket[] Buckets =NewBUCKET[NUM.LENGTH+1];//project to (0-N)     for(inti=0; i<buckets.length; i++) {Buckets[i]=NewBuckets ();//Initialize bucket    }     DoubleInterval = (Double) Num.length/(Max-min); //distribute every number to a bucket array     for(inti=0; i<num.length; i++){        intindex = (int) ((Num[i]-min) *interval); //put in a bucket        if(Buckets[index].low = =-1) {Buckets[index].low=Num[i]; Buckets[index].high=Num[i]; }Else{Buckets[index].low=math.min (Buckets[index].low, num[i]); Buckets[index].high=Math.max (Buckets[index].high, num[i]); }    }     //Scan buckets to find maximum gap    intresult = 0; intPrev = buckets[0].high;  for(intI=1; i<buckets.length; i++){        if(Buckets[i].low! =-1) {result= Math.max (Result, buckets[i].low-prev); Prev=Buckets[i].high; }     }     returnresult;}}

Lintcode: Maximum interval

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.