[leedcode] Maximum Gap

题目:(Sort)

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.

You may assume all elements in the array are non-negative integers and fit in the 32-bit signed integer range.

题解:

桶排序!!

public class Solution {
    public int maximumGap(int[] num) {
        if(num==null||num.length<2)
           return 0;

        int min=num[0];
        int max=num[0];

        for(int i:num)
        {
           min= Math.min(min,i);
           max= Math.max(max,i);
        }

        int gap = (int)Math.ceil((double)(max-min)/(num.length-1));
        int [] bucketMin = new int [num.length-1];
        int [] bucketMax = new int [num.length-1];

        Arrays.fill(bucketMin, Integer.MAX_VALUE);
        Arrays.fill(bucketMax, Integer.MIN_VALUE);

        for(int i:num)
        {
            if(i==min||i==max)
              continue;

            int index = (i-min)/gap ;
            bucketMin[index] = Math.min(i,bucketMin[index]);
            bucketMax[index] = Math.max(i,bucketMax[index]);
        }

        int prev =min;
        int maxGap = Integer.MIN_VALUE;
        for(int i=0 ; i<num.length-1; i++)
        {
            if(bucketMin[i]==Integer.MAX_VALUE||bucketMax[i]==Integer.MIN_VALUE)
               continue ;

            maxGap=Math.max(bucketMin[i]-prev,maxGap);
            prev=bucketMax[i];
        }

        maxGap = Math.max(max-prev,maxGap);
        return maxGap;
    }
}
时间: 2024-11-06 22:50:05

[leedcode] Maximum Gap的相关文章

164. Maximum Gap

/* * 164. Maximum Gap * 2016-6-4 by Mingyang * 这个题目首先要求的是linear的时间,所以我个人的预测就是bucketsort * bucketsort就是把一个list分成几个bucket再分别把每一个桶排序,再合起来 * 比如我现在有10个如果selection sort就是100的时间复杂度,那么需要分成两个5 * 就是两个25相加,就是50 * 假设有N个元素A到B. * 那么最大差值不会小于ceiling[(B - A) / (N - 1

LeetCode – Refresh – Maximum Gap

Sorting solution O(nlogn): 1 class Solution { 2 public: 3 int maximumGap(vector<int> &num) { 4 int len = num.size(), result = 0; 5 if (len < 2) return 0; 6 sort(num.begin(), num.end()); 7 for (int i = 0; i < len-1; i++){ 8 result = max(res

[LintCode] Maximum Gap 求最大间距

Given an unsorted array, find the maximum difference between the successive elements in its sorted form. Return 0 if the array contains less than 2 elements. Notice You may assume all elements in the array are non-negative integers and fit in the 32-

leetcode 155: Maximum Gap

Maximum Gap Total Accepted: 2946 Total Submissions: 12695 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 elemen

【leetcode 桶排序】Maximum Gap

1.题目 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. You may assume all elements in the array are non-

No.164 Maximum Gap

No.164 Maximum Gap 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. You may assume all elements in the

[leedcode 164] Maximum Gap

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. You may assume all elements in the array are non-negat

LeetCode[Sort]: Maximum Gap

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. You may assume all elements in the array are non-negat

Maximum Gap -- 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. You may assume all elements in the array are non-negat