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

思路

利用桶排序思想:
假设有N个元素数组array,最大值为Max,最小值为Min。
那么最大差值不会大于ceiling[(Maz - Min) / (N - 1)]。

令bucket(桶)的大小len = ceiling[(Maz - Min) / (N - 1)],则最多会有(Max - Min) / len + 1个桶
对于数组中的任意整数K,很容易通过算式index = (K - Min) / len找出其桶的位置,然后维护每一个桶的最大值和最小值。

由于同一个桶内的元素之间的差值至多为len - 1,因此最终答案不会从同一个桶中选择。
对于每一个非空的桶p,找出下一个非空的桶q,则q.min - p.max可能就是备选答案。返回所有这些可能值中的最大值。

代码

/*------------------------------------------------
*   日期:2015-03-23
*   作者:SJF0115
*   题目: 164.Maximum Gap
*   来源:https://leetcode.com/problems/maximum-gap/
*   结果:AC
*   来源:LeetCode
*   博客:
------------------------------------------------------*/
#include <iostream>
#include <cmath>
#include <climits>
#include <vector>
using namespace std;

class Solution {
public:
    int maximumGap(vector<int> &num) {
        if(num.empty() || num.size() < 2){
            return 0;
        }//if
        int size = num.size();
        // 统计最大值和最小值
        int Min = num[0];
        int Max = num[0];
        for(int i = 1;i < size;++i){
            if(num[i] > Max){
                Max = num[i];
            }//if
            if(num[i] < Min){
                Min = num[i];
            }//if
        }//for
        // 最大差值
        int gap = (int)ceil((double)(Max - Min)/(size - 1));
        // 桶个数
        int bucketNum = (int)ceil((double)(Max - Min)/gap);
        // 每个桶都维护一个最大值和最小值
        vector<int> bucketsMin(bucketNum, INT_MAX);
        vector<int> bucketsMax(bucketNum, INT_MIN);
        for(int i = 0; i < size;++i){
            if(num[i] == Max || num[i] == Min){
                continue;
            }//if
            // 位于哪个桶
            int index = (num[i] - Min) / gap;
            bucketsMin[index] = min(bucketsMin[index], num[i]);
            bucketsMax[index] = max(bucketsMax[index], num[i]);
        }//for
        // 计算最大差值
        int maxGap = 0;
        int pre = Min;
        for(int i = 0;i < bucketNum;++i){
            if(bucketsMin[i] == INT_MAX || bucketsMax[i] == INT_MIN){
                continue;
            }//if
            maxGap = max(maxGap, bucketsMin[i] - pre);
            pre = bucketsMax[i];
        }//for
        maxGap = max(maxGap,Max - pre);
        return maxGap;
    }
};

int main() {
    Solution solution;
    vector<int> vec = {4,2,1,9,5,11};
    cout<<solution.maximumGap(vec)<<endl;
    return 0;
}
AI 代码解读

运行时间

这里写图片描述

目录
相关文章
LeetCode 164. Maximum Gap
给定一个无序的数组,找出数组在排序之后,相邻元素之间最大的差值。 如果数组元素个数小于 2,则返回 0。
107 0
LeetCode 164. Maximum Gap
[LeetCode] Maximum Gap
This problem has a naive solution using sort and linear scan. The suggested solution uses the idea of bucket sort.
817 0
Maximum Gap
Given an unsorted array, find the maximum difference between the successive elements in its sorted form.
957 0
LeetCode 315. Count of Smaller Numbers After Self
给定一个整数数组 nums,按要求返回一个新数组 counts。数组 counts 有该性质: counts[i] 的值是 nums[i] 右侧小于 nums[i] 的元素的数量。
128 0
LeetCode 315. Count of Smaller Numbers After Self
[LeetCode] Largest Number
Well, this problem is designed for radix sort. For more information about radix sort, Introduction to Algorithms, 3rd edition has some nice examples.
667 0
LeetCode之Max Consecutive Ones
LeetCode之Max Consecutive Ones
147 0
LeetCode 414. Third Maximum Number
给定一个非空数组,返回此数组中第三大的数。如果不存在,则返回数组中最大的数。要求算法时间复杂度必须是O(n)。
151 0
LeetCode 414. Third Maximum Number
AI助理

你好,我是AI助理

可以解答问题、推荐解决方案等