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

思路:

这是一道难题,难点在于不能排序,但是要找到在排序的情况下相邻数字的最大差。

各种想不出来,很low的用了排序,居然也通过了。

然后看答案,发现可以很巧妙的利用桶的思想。找到数组里面的最大值和最小值,则数字间隔gap满足:

gap >= (max - min) / (N - 1)  结果向下取整  注意gap = 0 时取 1, 防止后面除0

然后,可以分为 (max - min) / gap + 1 个桶

每个数字根据  (num[i] - min) / gap 来决定放在哪个桶里。

记录每个桶里的最大值和最小值。

则最大间隔不会在桶内,而会在相邻的桶之间,bucket[i].min - bucket[i - 1].max 中最大的数字就是目标数字。

//这个虽然通过了,但是用了排序,是O(nlogn)的算法
    int maximumGap(vector<int> &num) {
        int ans = 0;
        sort(num.begin(), num.end());
        for(int i = 1; i < num.size(); i++)
        {
            ans = num[i] - num[i - 1];
        }
        return ans;
    }---------------------------------------------------------------------------------
    //答案的思路
    int maximumGap1(vector<int> &num) {
        if(num.size() < 2) return 0;
        //第一步,找最大和最小值
        int maxnum = num[0];
        int minnum = num[0];
        for(int i = 1; i < num.size(); i++)
        {
            maxnum = (maxnum < num[i]) ? num[i] : maxnum;
            minnum = (minnum > num[i]) ? num[i] : minnum;
        }
        //第二步:判断间隔的大小
        int gap = (maxnum - minnum) / (num.size() - 1);
        gap = (gap == 0) ? 1 : gap;
        //判断和记录每个桶的最大和最小数字
        vector<vector<int>> bucket((maxnum - minnum) / gap + 1, vector<int>(2, -1)); //只需记录每个桶的最大和最小数字
        for(int i = 0; i < num.size(); i++)
        {
            int belong = (num[i] - minnum) / gap;
            bucket[belong][0] = (num[i] > bucket[belong][0]) ? num[i] : bucket[belong][0]; //更新最大值
            bucket[belong][1] = (bucket[belong][1] < 0 || num[i] < bucket[belong][1]) ? num[i] : bucket[belong][1]; //更新最小值
        }

        //找最大间隔
        int ans = 0;
        int pre = 0;
        for(int i = 1; i < bucket.size(); i++)
        {
            if(bucket[i][0] == -1) continue;
            ans = (bucket[i][1] - bucket[pre][0] > ans) ? bucket[i][1] - bucket[pre][0] : ans;
            pre = i;
        }
        return ans;
    }

网上还有一种方法是利用基数排序,我还没看。

int maximumGap(std::vector<int> &num) {
    for(unsigned bit = 0; bit < 31; bit++)
    std::stable_partition(num.begin(), num.end(), [bit](int a){
        return !(a & (1 << bit));
    });
    int difference = 0;
    for(std::size_t i = 1; i < num.size(); i++) {
        difference = std::max(difference, num[i] - num[i-1]);
    }
    return difference;
}
时间: 2024-10-11 22:24:47

【leetcode】Maximum Gap(hard)★的相关文章

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

【LeetCode】 Maximum Depth of Binary Tree

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. 递归基础,不解释. class Solution { public: int getMax(TreeNode *root

【LeetCode】Maximum Product Subarray 求连续子数组使其乘积最大

Add Date 2014-09-23 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 = 

【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. 利用递归. 也可以使用栈和队列,即使用DFS和BFS方法. C++: 1 /** 2 * Definition for binary tree 3 * struct TreeNod

【leetcode】Maximum Subarray (53)

1.   Maximum Subarray (#53) 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 s

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

【Leetcode】Maximum Product Subarray

题目链接:https://leetcode.com/problems/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 t

【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. 思路: 很直接的最长上升子序列,线性 dp, dp 式