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. 什么是谷歌loon计划

    互联网服务已经与人类的生活密不可分,但受地理环境限制,目前全球只有三分之一的幸运儿能够体验到这种服务.为了让更多的人感受互联网,Google推出了一项名为“Project Loon”的计划,利用氢气球 ...

  2. POJ1141 Brackets Sequence

    Description Let us define a regular brackets sequence in the following way: 1. Empty sequence is a r ...

  3. [NOIP2011] 提高组 洛谷P1312 Mayan游戏

    题目描述 Mayan puzzle是最近流行起来的一个游戏.游戏界面是一个 7 行5 列的棋盘,上面堆放着一些方块,方块不能悬空堆放,即方块必须放在最下面一行,或者放在其他方块之上.游戏通关是指在规定 ...

  4. Mysql安全配置

    相关学习资料 http://drops.wooyun.org/tips/2245 http://www.cnblogs.com/siqi/archive/2012/11/21/2780966.html ...

  5. QA要懂的Linux命令

    <一>软件安装相关QA经常需要安装测试软件(jmeter.Mock.python环境搭建.java环境搭建),或者配置测试环境(nginx.ci等),需要了解linux下如何安装软件.在工 ...

  6. MySQL逻辑备份与恢复

    备份:mysqldump -uroot -p yyzc department > /home/admin/yyzc_backup.sql 恢复:mysql -uroot -p yyzc < ...

  7. BUAA1389愤怒的DZY(最大值最小化)

    http://acm.buaa.edu.cn/problem/1389/ 愤怒的DZY[问题描述]“愤怒的小鸟”如今已经是家喻户晓的游戏了,机智的WJC最近发明了一个类似的新游戏:“愤怒的DZY”.游 ...

  8. python urllib2使用心得

    python urllib2使用心得 1.http GET请求 过程:获取返回结果,关闭连接,打印结果 f = urllib2.urlopen(req, timeout=10) the_page = ...

  9. MS-sqlserver数据库2008如何转换成2000

    http://bbs.csdn.net/topics/390438560?page=1#post-394316973 MS-sqlserver数据库2008如何转换成2000 回你这个贴等于我写个博客 ...

  10. ECSHOP后台商品列表显示商品缩略图

    ECSHOP后台商品列表显示商品缩略图 ECSHOP教程/ ecshop教程网(www.ecshop119.com) 2013-11-06   ecshop 后台商品列表显示商品缩略图,大楷步凑如下: ...