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.

Credits:
Special thanks to @porker2008 for adding this problem and creating all test cases.

解题思路:

由于要用到线性时间复杂度,比较排序已经不适用了(《算法导论》P108),能够用到的有 计数排序、基数排序、堆排序,由于计数排序比较适合小范围的,基数排序最终会将顺序排好,而我们不需要那么复杂,因此,可以用桶排序,适当选择桶之间的距离,保证最大的successive distance在两桶之间即可,JAVA实现如下:

    public int maximumGap(int[] nums) {
if (nums.length <= 1)
return 0;
int min = nums[0], max = nums[0];
for (int num : nums) {
min = Math.min(min, num);
max = Math.max(max, num);
}
if (max == min)
return 0;
int distance = Math.max(1, (max - min) / (nums.length - 1));
int bucket[][] = new int[(max - min) / distance + 1][2];
for (int i = 0; i < bucket.length; i++) {
bucket[i][0] = Integer.MAX_VALUE;
bucket[i][1] = -1;
}
for (int num : nums) {
int i = (num - min) / distance;
bucket[i][0] = Math.min(num, bucket[i][0]);
bucket[i][1] = Math.max(num, bucket[i][1]);
}
int maxDistance = 1, left = -1, right = -1;
for (int i = 0; i < bucket.length; i++) {
if (bucket[i][1] == -1)
continue;
if (right == -1) {
right = bucket[i][1];
continue;
}
left = bucket[i][0];
maxDistance=Math.max(maxDistance, left-right);
right=bucket[i][1];
}
return maxDistance;
}

Java for 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

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

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

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

  4. [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. 164. Maximum Gap

    题目: Given an unsorted array, find the maximum difference between the successive elements in its sort ...

随机推荐

  1. Lucene -- 实时索引

    lucene的实时搜索可以分成:实时和近实时的搜索. 实时只能依靠内存了. 近实时可以用lucene中提供org.apache.lucene.index.DirectoryReader.open(In ...

  2. Android Launcher分析和修改9——Launcher启动APP流程

    本来想分析AppsCustomizePagedView类,不过今天突然接到一个临时任务.客户反馈说机器界面的图标很难点击启动程序,经常点击了没有反应,Boss说要优先解决这问题.没办法,只能看看是怎么 ...

  3. 【SDOI2008】解题汇总

    好叭我真的是闲的了... /---------------------------------------------/ BZOJ-2037 [SDOI2008]Sue的小球 DP+相关费用提前计算 ...

  4. groovy-集合

    Lists 你能使用下面的方法创建一个lists,注意[]是一个空list. 1 def list = [5, 6, 7, 8] 2 assert list.get(2) == 7 3 assert  ...

  5. HTTPS 协议降级攻击原理

    0x00 HTTPS 在传统流行的web服务中,由于http协议没有对数据包进行加密,导致http协议下的网络包是明文传输,所以只要攻击者拦截到http协议下的数据包,就能直接窥探这些网络包的数据. ...

  6. 使用 Python 抓取欧洲足球联赛数据

    Web Scraping在大数据时代,一切都要用数据来说话,大数据处理的过程一般需要经过以下的几个步骤    数据的采集和获取    数据的清洗,抽取,变形和装载    数据的分析,探索和预测    ...

  7. PHP局部变量与全局变量

    一.局部变量定义:在函数内部声明,且只能在函数内部调用的变量. 注意:参数也是局部变量的一种. demo1:1 function demo1(){2     $age = 10;3 }4 5 echo ...

  8. yum被锁Another app is currently holding the yum lock; waiting for it to exit...

    可能是系统自动升级正在运行,yum在锁定状态中. 可以通过强制关掉yum进程: #rm -f /var/run/yum.pid 然后就可以使用yum了.

  9. 锋利的jQuery-1--end()

    1.导航菜单:选中后显示当前标签下的子标签并且高亮,并且隐藏其他同级标签,(一行级联操作即完成),主要看end()函数用法. end(): 回到最近的一个"破坏性"操作之前.就是指 ...

  10. php 命名空间

    命名空间一个最明确的目的就是解决重名问题,PHP中不允许两个函数或者类出现相同的名字,否则会产生一个致命的错误.这种情况下只要避免命名重复就可以解决,最常见的一种做法是约定一个前缀. 例:项目中有两个 ...