LeetCode 笔记28 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-negative integers and fit in the 32-bit signed integer range.

这道题题目有提示。

提示1 有线性时间复杂度解法

提示2 非负数,且强调32位整数

首先想排序的话,线性时间复杂度就那么几个解法,位图和基数排序。显然这个不能用位图,空间消耗太大。其实在看Algorithm 4th edition的时候就想,基数排序真是强大,完全可以用来做这个么。

不过当时我忘记基数排序的counting方法了。复习了一下才记起来。

public int maximumGap(int[] num) {
        if (num == null || num.length <= 1) {
            return 0;
        }

        for (int d = 0; d < 32; d++) {
            int[] count = new int[3];
            int[] aux = new int[num.length];
            for (int i = 0; i < num.length; i++) {
                count[((num[i] >> d) & 1) + 1]++;
            }

            for (int i = 1; i < 2; i++) {
                count[i] += count[i - 1];
            }

            for (int i = 0; i < num.length; i++) {
                aux[count[((num[i] >> d) & 1)]++] = num[i];
            }

            for (int i = 0; i < num.length; i++) {
                num[i] = aux[i];
            }
        }
        int maxGap = 0;
        for (int i = 1; i < num.length; i++) {
            if (num[i] - num[i - 1] > maxGap) {
                maxGap = num[i] - num[i - 1];
            }
        }
        return maxGap;
    }

count数组的意义具体可以参考上面提到的书关于String Sort的第一部分。

然后看了下leetcode的标准答案,原来用的桶排序。这个也很强大。

假设数组中最大元素是Max, 最小元素Min,数组的长度是len,那么相邻两个数的平均间隔是D = (Max - Min)/(len - 1)。相邻两个数的最大间隔肯定大于等于这个数值。

那么我们不妨假设[Min, Max]之间所有数都可以放在紧紧排列的一个个桶中。每个桶的大小就是D。桶内元素是之间的间隔肯定不是要求的最大间隔,而是前一个桶中的最大值和后面一个桶的最小值才可能是最大间隔。我们遍历这样的值,就可以找出最大间隔。

桶的个数么,就是(Max - Min) / D + 1。

值为K的元素呢,就属于第(K - Min) / D 个桶里面了。

我们遍历一次数组,把每个桶都填上数组中的元素,同时可以求得每个桶的最大最小值。

再遍历一次桶,就求得了那个最大的间隔。

最后注意,如果len算出来是0,那么桶的个数就等于元素个数。

public int maximumGap2(int[] num) {
        if (num == null || num.length <= 1) {
            return 0;
        }
        int max = Integer.MIN_VALUE;
        int min = Integer.MAX_VALUE;
        for (int i = 0; i < num.length; i++) {
            if (num[i] > max) {
                max = num[i];
            }
            if (num[i] < min) {
                min = num[i];
            }
        }
        int len = (max - min) / (num.length - 1);
        if (len == 0) {
            len = 1;
        }
        int numOfBucket = (max - min) / len + 1;
        Bucket[] buckets = new Bucket[numOfBucket];
        for (int i = 0; i < num.length; i++) {
            int idx = (num[i] - min) / len;
            if (buckets[idx] == null) {
                buckets[idx] = new Bucket();
            }
            if (num[i] > buckets[idx].max) {
                buckets[idx].max = num[i];
            }
            if (num[i] < buckets[idx].min) {
                buckets[idx].min = num[i];
            }
        }
        int maxGap = 0;
        max = -1;
        for (int i = 0; i < buckets.length; i++) {
            if (buckets[i] != null) {
                if (max == -1) {
                    //pass
                } else {
                    maxGap = Math.max(buckets[i].min - max, maxGap);
                }
                max = buckets[i].max;
            }
        }
        return maxGap;
    }

However,我发现桶排序做出来好像比基数排序慢也。

时间: 2024-10-25 22:13:27

LeetCode 笔记28 Maximum Gap的相关文章

leetcode笔记:Maximum Depth of Binary Tree

一. 题目描述 Given a binary tree, find its maximum depth. The maximum depth is the number of nodes along the longest path from the root node down to the farthest leaf node. 二. 题目分析 这道题和Minimum Depth of Binary Tree一题相似,这个是求最大深度的,就是对二叉树进行递归,然后将子树的最大深度进行返回,最

leetcode笔记:Maximum Product of Word Lengths

一. 题目描述 Given a string array words, find the maximum value of length(word[i]) * length(word[j]) where the two words do not share common letters. You may assume that each word will contain only lower case letters. If no such two words exist, return 0.

LeetCode:164. 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. 中文翻译: 现在有一个无序数组,找出数组在排序后,相邻元素差值的最大值 如果元素个数少于两个,就返回0. emmmmm

LeetCode 笔记26 Maximum Product Subarray

Find the contiguous subarray within an array (containing at least one number) which has the largest product. For example, given the array [2,3,-2,4],the contiguous subarray [2,3] has the largest product = 6. mlgb的,今天遇到这个题搞了半天.记录下. 首先解放可以使用穷举.通过二维数组pr

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 桶排序】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 Depth of Binary Tree

Given a binary tree, find its maximum depth. The maximum depth is the number of nodes along the longest path from the root node down to the farthest leaf node. 题解:递归,树的高度 = max(左子树高度,右子树高度)+1: 代码如下: 1 /** 2 * Definition for binary tree 3 * public cla

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笔记

leetcode 笔记 Linked List 2. Add Two Numbers You are given two non-empty linked lists representing two non-negative integers. The digits are stored in reverse order and each of their nodes contain a single digit. Add the two numbers and return it as a