Leetcode (164) Maximum Gap

Source: Internet
Author: User

The topics are as follows:

Given an unsorted array, find the maximum difference between the successive elements in its sorted form.
Try to solve it in linear time/space.
Return 0 if the array contains less than 2 elements.
The May assume all elements in the array are non-negative integers and fit in the 32-bit integer range.

The analysis is as follows:

There are a number of ways to think about using bucket sort or radix sort. Then put the dog a search found sure enough to use the bucket sort.

The basic idea is this, put the number of n in the X bucket. We can construct a suitable x and find out from the following proof that Max Gap can only appear between two barrels and cannot appear inside a barrel.

Here is the proof, directly copy the official answer:

Suppose there are N elements and they range from A to B.

Then The maximum gap would be no smaller than ceiling[(B-A)]

Let the length of a bucket to be len = ceiling[(b-a)/(N-1)], then we'll have at most num = (b-a)/len + 1 of BU Cket


For all number K in the array, we can easily find out which bucket it belongs by calculating loc = (k-a)/Len and there Fore maintain the maximum and minimum elements in each bucket.

Since the maximum difference between elements in the same buckets would be at most len-1 and so the final answer won't be Taken from two elements in the same buckets.

For each non-empty buckets p and find the next non-empty buckets Q then Q.min-p.max could is the potential to the Q Uestion. Return to the maximum of all those values.


My Code:

48ms struct Bucket {int local_min;
    int Local_max;
    int local_volumn;
Bucket (): Local_volumn (0) {};
};
        Class Solution {public:int maximumgap (vector<int> &num) {if (Num.size () < 2) return 0; if (num.size () = = 2) return num[0]-num[1] > 0?
        NUM[0]-num[1]: num[1]-num[0];
        int min = Int_max;
        int max = Int_min;
        int max_gap = 0;
            for (int i = 0; i < num.size (); ++i) {if (Num[i] < min) min = num[i];
        if (Num[i] > max) max = num[i]; int bucket_length = (max-min)/(Num.size ()-1) + 1; Maximum Gap >= ceiling[(b-a)/(N-1)]//int bucket_length = (max-min)/num.size () + 1;
        I can also ac ... int bucket_count = (max-min)/bucket_length + 1;
        Vector<bucket> Bucket (Bucket_count, Bucket ());
            for (int i = 0; i < num.size (); ++i) {int index = (num[i]-min)/bucket_length; if (bucket[inDex].local_volumn = 0)//note1: If there is at most one element in the bucket, then Max and Min are assigned this element.
            Bucket[index].local_max = Bucket[index].local_min = Num[i];
            else if (Num[i] > Bucket[index].local_max) Bucket[index].local_max = Num[i];
            else if (Num[i] < bucket[index].local_min) bucket[index].local_min = Num[i];

        bucket[index].local_volumn++; int previous = 0;
        NOTE2: Previous to skip empty barrels, the No. 0 bucket is not empty bucket, so previous can start from 0 value.
            for (int i = 1; i < bucket.size (); ++i) {if (Bucket[i].local_volumn = 0) continue; if (Bucket[i].local_min-bucket[previous].local_max > Max_gap) max_gap = Bucket[i].local_min-bucke
            T[previous].local_max;
        previous = i;
    return max_gap;

 }
};

Summary extension:

The basic idea of 1 sort can expand a lot of content.

2 after testing found

Bucket_length = (max-min)/(Num.size ()-1) + 1;
If it is written as

Bucket_length = (max-min)/num.size () + 1;

can also be AC.

But in a sense, the 1th way I think is right. The 2nd type has not yet thought of a counter case to overthrow, but then the maximum gap will be no smaller than ceiling[(B-A)], so in a sense I feel incorrect.

The 3 topic said 32-bit, I feel strongly hinted that can use bit way to carry on radix sort, this idea has not realized.

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.