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.
Problem: Given an unordered array, find the maximum interval of adjacent elements after the array is sorted. Requires O (n) time complexity, O (n) space complexity
Problem Solving Ideas:
Idea one: All arrays are sorted, and once again the maximum number of adjacent element intervals is computed. But the minimum time complexity of the sorting algorithm also requires O (N*LOGN), which cannot meet the requirements.
Idea two: The topic only requires the largest adjacent interval, you can use the bucket to sort, to avoid finding all the element size order, and get results. The time complexity is reduced to O (n).
Specific ideas:
- Subtract the minimum value from all elements to get a new array
- Create a bucket of the same size (array representation) based on the length of the array, and get the size of a single bucket based on the array's numeric range and array length
- Double bucketsize = (double) (MAXV-MINV)/(double) nums.size ();
- Depending on the element size, the element is placed inside the corresponding bucket. The maximum value of the array, the minimum value should be in the leftmost, the rightmost two barrels inside.
- int idx = newnums[i]/bucketsize;
- Bucket[idx].push_back (Newnums[i]);
Assertion: The maximum interval value is the maximum value of the interval between buckets.
Assertion proves that when each bucket has a value, there is only one value per bucket, and the assertion is true. When at least one bucket is empty, and the leftmost and rightmost two buckets have values, the maximum interval is necessarily the interval between buckets, not the interval within the bucket, and the assertion is established.
- Remove values other than the maximum and minimum values in each bucket
- The maximum bucket interval is calculated by one traversal, which is the original.
1 intMaximumgap (vector<int>&nums) {2 3 if(Nums.size () <2) {4 return 0;5 }6 7 intMAXV = nums[0];8 intMINV = nums[0];9 for(inti =0; I < nums.size (); i++) {TenMAXV =Max (MAXV, nums[i]); OneMINV =min (MINV, nums[i]); A } - -vector<int>newnums; the for(inti =0; I < nums.size (); i++) { -Newnums.push_back (Nums[i]-MINV); - } - +vector<vector<int>> Buckets (nums.size () +1); - + DoubleBucketsize = (Double) (MAXV-MINV)/(Double) nums.size (); A at //MAXV = = Minv - if(Bucketsize = =0 ) { - return 0; - } - - for(inti =0; I < newnums.size (); i++) { in intIDX = newnums[i]/bucketsize; - Bucket[idx].push_back (Newnums[i]); to } + - for(inti =0; I < bucket.size (); i++) { the if(Bucket[i].size () >=2) { * intMaxi = bucket[i][0]; $ intMini = bucket[i][0];Panax Notoginseng for(intK =0; K < Bucket[i].size (); k++ ) { -Maxi =Max (Maxi, Bucket[i][k]); theMini =min (Mini, bucket[i][k]); + } ABucket[i] ={Mini, Maxi}; the } + } - $ intMaxgap =0; $ - intLmaxi = (bucket[0].size () = =1) ? bucket[0][0]: bucket[0][1]; - the for(inti =0; I < bucket.size (); i++) { - if(bucket[i].size () = =0 ) {Wuyi Continue; the } - intMaxi; Wu intMini; - About if(bucket[i].size () = =1) { $Maxi = bucket[i][0]; -Mini = bucket[i][0]; -}Else{ - //size of bucket[i] shoud be 2; A +Mini = bucket[i][0]; theMaxi = bucket[i][1]; - } $ the if((Mini-lmaxi) >maxgap) { theMaxgap = (mini-Lmaxi); the } theLmaxi =Maxi; - } in the returnMaxgap; the}
[Leetcode] Maximum Gap Problem Solving ideas