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.

Example 1:

Input: [3,6,9,1]
Output: 3
Explanation: The sorted form of the array is [1,3,6,9], either
  (3,6) or (6,9) has the maximum difference 3.

Example 2:

Input: [10]
Output: 0
Explanation: The array contains less than 2 elements, therefore return 0.

Note:

  • You may assume all elements in the array are non-negative integers and fit in the 32-bit signed integer range.
  • Try to solve it in linear time/space.

遇到这类问题肯定先想到的是要给数组排序,但是题目要求是要线性的时间和空间,那么只能用桶排序或者基排序。这里用桶排序 Bucket Sort 来做,首先找出数组的最大值和最小值,然后要确定每个桶的容量,即为 (最大值 - 最小值) / 个数 + 1,在确定桶的个数,即为 (最大值 - 最小值) / 桶的容量 + 1,然后需要在每个桶中找出局部最大值和最小值,而最大间距的两个数不会在同一个桶中,而是一个桶的最小值和另一个桶的最大值之间的间距,这是因为所有的数字要尽量平均分配到每个桶中,而不是都拥挤在一个桶中,这样保证了最大值和最小值一定不会在同一个桶中,具体的证明博主也不会,只是觉得这样想挺有道理的,各位看官大神们若知道如何证明请务必留言告诉博主啊,参见代码如下:

class Solution {
public:
int maximumGap(vector<int>& nums) {
if (nums.size() <= ) return ;
int mx = INT_MIN, mn = INT_MAX, n = nums.size(), pre = , res = ;
for (int num : nums) {
mx = max(mx, num);
mn = min(mn, num);
}
int size = (mx - mn) / n + , cnt = (mx - mn) / size + ;
vector<int> bucket_min(cnt, INT_MAX), bucket_max(cnt, INT_MIN);
for (int num : nums) {
int idx = (num - mn) / size;
bucket_min[idx] = min(bucket_min[idx], num);
bucket_max[idx] = max(bucket_max[idx], num);
}
for (int i = ; i < cnt; ++i) {
if (bucket_min[i] == INT_MAX || bucket_max[i] == INT_MIN) continue;
res = max(res, bucket_min[i] - bucket_max[pre]);
pre = i;
}
return res;
}
};

Github 同步地址:

https://github.com/grandyang/leetcode/issues/164

参考资料:

https://leetcode.com/problems/maximum-gap

http://blog.csdn.net/u011345136/article/details/41963051

https://leetcode.com/problems/maximum-gap/discuss/50642/radix-sort-solution-in-java-with-explanation

https://leetcode.com/problems/maximum-gap/discuss/50643/bucket-sort-java-solution-with-explanation-on-time-and-space

LeetCode All in One 题目讲解汇总(持续更新中...)

[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. [LintCode] Maximum Gap 求最大间距

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

  3. leetcode[164] Maximum Gap

    梅西刚梅开二度,我也记一题. 在一个没排序的数组里,找出排序后的相邻数字的最大差值. 要求用线性时间和空间. 如果用nlgn的话,直接排序然后判断就可以了.so easy class Solution ...

  4. [LeetCode] Maximum Gap 求最大间距

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

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

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

  6. Java for LeetCode 164 Maximum Gap

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

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

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

  8. 【刷题-LeetCode】164 Maximum Gap

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

  9. 【leetcode】Maximum Gap

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

随机推荐

  1. 造轮子ArrayList

    这篇博客实现一个简单的ArrayList集合.博客里的代码首先根据自己的想法实现,在走不动的情况下会去参考JDK源代码.所以阅读本文,不要抱着跟JDK源码对比的心态.于我个人而言,国庆期间,纯属娱乐. ...

  2. navcat搜索字符串方法

    navcat搜索字符串方法右键点击数据库 查找字符串即可..

  3. Win10+Anaconda+tensorflow-cpu安装教程

    基础概念 Python2.x or Python3.x 自从20世纪90年代初Python语言诞生至今,一直在迭代更新,根据出现的时期,可以分为Python2.x和Python3.x两个大版本.其中P ...

  4. 解决用navicat远程连接数据库出现1045 access denied for user 'root'@'localhost' using password yes

    在mysql命令行中执行 SET PASSWORD FOR 'root'@'localhost' = PASSWORD('123456XXX');  GRANT ALL PRIVILEGES ON * ...

  5. centos6 cgroup及cgred简介和简单使用

    一.cgroup简介 Linux CGroup全称Linux Control Group, 是Linux内核的一个功能,用来限制,控制与分离一个进程组群的资源(如CPU.内存.磁盘输入输出等).这个项 ...

  6. ExcelHelper based on NPOI

    //Export data to excel via NPOI public static void ExportDataTableToExcel(DataTable dataTable, strin ...

  7. 微信和QQ可以关闭广告了,每次能关6个月

    微信和QQ可以关闭广告了,这次腾讯真的是良心了,虽然不能完全关闭,但是每次能关6个月,也能清静不少时间. 关闭地址:点击进入

  8. badboy录制过程中出现当前页面的脚本发现错误

    为什么出现这个提示 , 是因为访问者使用的浏览器不能完全支持页面里的脚本,毕竟版本太老,一直没有更新 ,这个版本错误并不会影响使用,有强迫症的可以关闭下,

  9. 纯C语言实现循环双向链表创建,插入和删除

    #include <stdio.h> #include <stdlib.h> typedef int ElemType; typedef struct DLNode{ Elem ...

  10. Linux搭建www,mail,ftp三大DNS服务器

    ##############################-----服务器端----###############################1. 安装bind# yum install bin ...