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 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], max = num[0];
        int len = num.length;
        for(int i=1; i<len; i++) {
            if(num[i] < min) {
                min = num[i];
            } else if(num[i] > max) {
                max = num[i];
            }
        }
        if(len==2) return max - min;
        int [] min_bs = new int[len+1];
        int [] max_bs = new int[len+1];

        for(int i=0; i<len; i++) {
            int x = num[i];
            int k =  (int)(len * (1.0 * (x - min) / (max - min))); //attention! may have overflow problem!
            if(min_bs[k]==0 || x<min_bs[k]) min_bs[k] = x;
            if(max_bs[k]==0 || x>max_bs[k]) max_bs[k] = x;
        }

        int maxInter = 0;
        min = max_bs[0];
        for(int i=1; i<len+1; i++) {
            if(min_bs[i] == 0) continue;
            maxInter = Math.max(maxInter, min_bs[i] - min);
            min = max_bs[i];
        }
        return maxInter;

    }
}
时间: 2024-12-23 02:33:16

leetcode 155: Maximum Gap的相关文章

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

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

【leetcode】Maximum Gap(hard)★

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

Java for LeetCode 163 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】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 164:Maximum Gap

题意: 给定非排序数组,找出其排序后相邻元素的最大差值. 线性时间空间.元素数少于2时返回0.元素值非负且int范围内. 思路: 排序最快nlogn不符合要求: 参考网上,学习了桶排序的方法: 桶排序:按值分段处理: 设定桶大小和桶个数: 因为ans>=(MAX-MIN)/(len-1): 桶大小:(MAX-MIN)/(len-1)向上取整,(注意为0时取1) 桶个数:(MAX-MIN)/桶大小+1: ans取值只可能是桶A的最小值-桶B的最大值形式:不会在桶内取:(因为桶内最大差值小于桶大小)

LeetCode 164. Maximum Gap (排序)

题目 题意:就是给你一个数组,让你输出排好序后,相邻元素差值最大的那个差值. 题解:首先,当然我们可以用快排,排完序之后,遍历一遍数组,就能得到答案了.但是快速排序的效率是O(n* logn),不是题目要求的线性效率,也就是O(n)的效率. 那么诸多排序算法中,也是由线性效率的排序算法,当然这些算法的目标是线性效率,而在实际情况中的效率也并不是最快的.首先就是基数排序,基数排序的效率是O(n*k), k 是由元素的位数决定的. 线性时间的排序还有一个桶排序,桶排序,一开始选择一些桶,每个桶代表一

【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-

【LeetCode】 Maximum Subarray

Find the contiguous subarray within an array (containing at least one number) which has the largest sum. For example, given the array [−2,1,−3,4,−1,2,1,−5,4], the contiguous subarray [4,−1,2,1] has the largest sum = 6. More practice: If you have figu