"Bucket sort" to find the largest "Gap" in an unordered array

Source: Internet
Author: User

Title: Leetcode

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.
Assume all elements in the array is non-negative integers and fit in the 32-bit signed integer range.

Tips:

Suppose the sorted array in the title is ascending.

Using bucket sorting, the array is divided evenly into the Len group (Len is the number of array elements), each group is closed before opening, where the last group has only one element-the maximum value.

There are two possible sources for the largest gap: 1, the difference between the maximum and minimum values in each bucket, and the difference between 2, two adjacent non-empty buckets, and the "bucket minimum" minus the "bucket maximum" value.

The return value first is the array minimum, the second number is the array maximum value Pair<int, int> minmax (const vector<int> &s) {int rmin = Int_max, Rmax = -1;for (s ize_t i = 0; I < s.size (); i++) {rmin = min (rmin, s[i]); rmax = max (Rmax, s[i]);} Pair<int, int> res = {rmin, rmax};return Res;} Ask for the largest of the three numbers int maxnum (const int &AMP;A, const int &AMP;B, const int &c) {int temp = max (A, b); temp = max (temp, c); Retu RN temp;} The elements of the array s are divided evenly into s.size () groups, each set closed behind, i.e. similar to [a, b] vector<vector<int>> bucketsort (const vector<int> &s, const int size) {pair<int, int> r = Minmax (s);vector<vector<int>> bucket (size); for (size_t i = 0; i < S.size (); i++) {int index = (s[i]-R.first)/Size;bucket[index].push_back (S[i]);} return bucket;} int Findmaxgap (const vector<int> &s) {if (S.size () < 2) return 0;vector<vector<int>> bucket = Bucketsort (S,s.size ()); int gap, premax;//initialize Gap and Premaxif (Bucket[0].size () >= 2) {Auto T = Minmax (bucket[0]); gap = T.second-t.first;premax = T.second;} else if (Bucket[0].empty ()) {gap = -1;premax = 0;} Else{gap = -1;premax = Bucket[0][0];} for (size_t i = 1; i < bucket.size (); i++) {if (Bucket[i].empty ()) continue;if (bucket[i].size () = = 1) {gap = Max (Gap, Buc Ket[i][0]-Premax);p Remax = bucket[i][0];continue;} Pair<int, int> t = Minmax (Bucket[i]), Gap = Maxnum (Gap, T.second-t.first, T.first-premax);p Remax = T.second;} return gap;} Test Code Srand ((unsigned) time (NULL));vector<int> n;for (int i = 0; i < i++) {int t = rand ()% + 1;n.push_back (t); cout << t << "\ T";}  cout << endl;cout << findmaxgap (n) << Endl;sort (N.begin (), N.end ()); for (int i = 0; i < ten; i++) {cout << n[i] << "\ t";}


"Bucket sort" to find the largest "Gap" in an unordered array

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.