梅西刚梅开二度,我也记一题。

在一个没排序的数组里,找出排序后的相邻数字的最大差值。

要求用线性时间和空间。

如果用nlgn的话,直接排序然后判断就可以了。so easy

class Solution {
public:
int maximumGap(vector<int> &num) {
if (num.size() < ) return ;
sort(num.begin(), num.end());
int maxm = -;
for (int i = ; i < num.size(); i++)
{
if (abs(num[i] - num[i-]) > maxm)
maxm = abs(num[i] - num[i-]);
}
return maxm;
}
};

但我们要的是线性时间。

其实这个思想在算法课上有讲过。用桶的思想。把数组分成几个桶,然后判断相邻桶的最大与最小之间的差值。关键是要知道每个桶的长度,已经桶的个数。

class Solution {
public:
int maximumGap(vector<int> &num) {
if (num.size() < ) return ;
int Max = num[], Min = Max, ans = ;
for (int i = ; i < num.size(); i++)
{
if (num[i] < Min)
Min = num[i];
if (num[i] > Max)
Max = num[i];
}
if (Max == Min) return ; int bucketGap = (Max - Min)/num.size() + ; // 桶的间隔是关键
int bucketLen = (Max - Min)/bucketGap + ; // 举个 1,2,3的例子就知道了
vector<int> MinMax(, INT_MAX);
MinMax[] = INT_MIN;
vector<vector<int> > bucket(bucketLen, MinMax); for (int i = ; i < num.size(); i++)
{
int ind = (num[i] - Min)/bucketGap;
if (num[i] < bucket[ind][])
bucket[ind][] = num[i];
if (num[i] > bucket[ind][])
bucket[ind][] = num[i];
} int first = bucket[][], second;
for (int i = ; i < bucketLen; i++)
{
if (bucket[i][] == INT_MAX) continue;
second = bucket[i][];
int tmpmax = second - first;
ans = tmpmax > ans ? tmpmax : ans;
first = bucket[i][];
} return ans;
}
};

最后附上官网的解法说明:

Suppose there are N elements and they range from A to B.

Then the maximum gap will be no smaller than ceiling[(B - A) / (N - 1)]

Let the length of a bucket to be len = ceiling[(B - A) / (N - 1)], then we will have at most num = (B - A) / len + 1 of bucket

for any number K in the array, we can easily find out which bucket it belongs by calculating loc = (K - A) / len and therefore maintain the maximum and minimum elements in each bucket.

Since the maximum difference between elements in the same buckets will be at most len - 1, so the final answer will not be taken from two elements in the same buckets.

For each non-empty buckets p, find the next non-empty buckets q, then q.min - p.max could be the potential answer to the question. Return the maximum of all those values.

leetcode[164] Maximum Gap的更多相关文章

  1. LeetCode 164. Maximum Gap[翻译]

    164. Maximum Gap 164. 最大间隔 Given an unsorted array, find the maximum difference between the successi ...

  2. [LeetCode] 164. Maximum Gap 求最大间距

    Given an unsorted array, find the maximum difference between the successive elements in its sorted f ...

  3. ✡ leetcode 164. Maximum Gap 寻找最大相邻数字差 --------- java

    Given an unsorted array, find the maximum difference between the successive elements in its sorted f ...

  4. Java for LeetCode 164 Maximum Gap

    Given an unsorted array, find the maximum difference between the successive elements in its sorted f ...

  5. 【LeetCode】164. Maximum Gap (2 solutions)

    Maximum Gap Given an unsorted array, find the maximum difference between the successive elements in ...

  6. 【刷题-LeetCode】164 Maximum Gap

    Maximum Gap Given an unsorted array, find the maximum difference between the successive elements in ...

  7. 【leetcode】Maximum Gap

    Maximum Gap Given an unsorted array, find the maximum difference between the successive elements in ...

  8. 【Leetcode】164. Maximum Gap 【基数排序】

    Given an unsorted array, find the maximum difference between the successive elements in its sorted f ...

  9. 【leetcode】Maximum Gap(hard)★

    Given an unsorted array, find the maximum difference between the successive elements in its sorted f ...

随机推荐

  1. Android 墙纸设置代码 详细说明

    使游戏图像列表.思考添加壁纸功能.我发了一些资料. 1 别忘记在ApplicationManifest.xml 中加上权限的设置. <uses-permission android:name = ...

  2. Windows Phone APP中禁用截图

    原文:Windows Phone APP中禁用截图 Windows Phone 8 有系统自带的截图功能,快捷键:电源键+Win键,可以随意截图. Windows Phone 更新GDR2后新增了一个 ...

  3. 求N!末尾的0的个数(找规律+递归)

    0\'s Time Limit: 1000ms   Memory limit: 65536K  有疑问?点这里^_^ 题目描写叙述 计算整数n!(n的阶乘)末尾有多少个0. 输入 第一行输入一个数T代 ...

  4. 皴linux rootpassword(方式:重置rootpassword)

    皴linux rootpassword: 开机后,.点击"e"进入维护模式.选"内核选项",例如,看到下面的数字: watermark/2/text/aHR0c ...

  5. 五通信算法:五种编码增益比较matlab模拟

    1. 卷积编码增益性能.BER 信道环境:AWGN 信噪比SNR :0:0.1:6 MATALB仿真架构:源比特 +卷积码 +BPSK +AWGN +Viterbi +BER 说明:卷积编码,不同的R ...

  6. 新安装Win10

    随着微软发布Windows 10下载技术预览版.现在,您可以免费下载Windows 10技术预览ISO档,安装和开放经验. Windows 10技术预览提供的第一部英语.中国简体.葡萄牙语,含32位. ...

  7. 编程算法基础-数字数码管-隐藏password

    作业 数字数码管 个数码管) @@@@         0     @          @       1   2     @          @         3        @@@@    ...

  8. scala 101

    * scala 安装: 下载可以执行的文件. 注意版本.  spark 0.8.0 对应的scala 为2.9.3 * scala 编译: 和java 很像: 1,  直接编译脚本: scalac H ...

  9. 锋利的KATANA

    锋利的KATANA 阅读目录 ASP.NET 发展历程 走进Katana的世界 使用ASP.NET/IIS托管Katana-based应用程序 使用自定义Host(self-host)托管Katana ...

  10. CSDN下载频道2014年11月4日本-5日常维护公告

    尊敬的用户: CSDN于2005年推出了下载服务.经过数年的发展.下载频道的用户已经为无数用户提供了帮助,分享500万的技术资源. CSDN下载频道将于2014年11月4日23点至11月5日8点进行积 ...