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

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

要求用线性时间和空间。

如果用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. HDOJ 4248 A Famous Stone Collector DP

    DP: dp[i][j]前i堆放j序列长度有多少行法, dp[i][j]=dp[i-1][j] (不用第i堆), dp[i][j]+=dp[i-1][j-k]*C[j][k] (用第i堆的k个石头) ...

  2. Notification使用以及PendingIntent.getActivity() (转)

    public void sendNotification(Context ctx,String message) { //get the notification manager String ns ...

  3. 王立平-NGUI

    NGUI: UI插入 UI : 人机交互界面 UI大部分是2D 眼下就我们总结这些.也许会增加 版权声明:本文博主原创文章,博客,未经同意不得转载.

  4. easyUI的combobox实现级联

    先简介下combobox: easyUI重写了select,取而代之的是combobox,有例如以下几种方式能够创建一个combobox 1.使用select标签,并加上class="eas ...

  5. 第2章 简单工厂模式(Sample Factory)

    原文 第2章 简单工厂模式(Sample Factory) 一般用到的场景:对象多次被实例引用,切有可能会发生变化 拿我们的简单三层举例子 先定义dal层 1 2 3 4 5 6 7 8     cl ...

  6. Eclipse UML 工具 ObjectAid 介绍

    概要 本文介绍如何使用 ObjectAid(http://www.objectaid.com/) UML Explorer 创建 UML 图,高速阅读代码. 安装 ObjectAid ObjectAi ...

  7. WebAPI客户端

    封装WebAPI客户端,附赠Nuget打包上传VS拓展工具 一.前言 上篇< WebAPI使用多个xml文件生成帮助文档 >有提到为什么会出现基于多个xml文件生成帮助文档的解决方案,因为 ...

  8. MSMQ-发送消息到远程专用队列 实例

    目录 一:MSMQ的一些理论上的知识 二:队列类型(Queue Type) 三:安装消息队列 四:在C#中Messagequeue class 五:MSMQ-发送消息到远程专用队列 六:例子   一. ...

  9. Java对多线程~~~Fork/Join同步和异步帧

    于Fork/Join骨架,当提交的任务,有两个同步和异步模式.它已被用于invokeAll()该方法是同步的.是任何 务提交后,这种方法不会返回直到全部的任务都处理完了.而还有还有一种方式,就是使用f ...

  10. MAC使用小技巧(二)

    一.Safari-->广告数量不足 --原因:DNS被拦截,被恶意推送广告. ----------------------------- [ 思路 ] 修改hosts文件 $ cd /etc $ ...