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.

给定一个未排序的数组,然后找出排序之后的数组中,相邻数字的最大差。

1、桶排序

public class Solution {
public int maximumGap(int[] nums) {
int len = nums.length;
if (len < 2){
return 0;
}
int max = nums[0];
int min = nums[0];
for (int num : nums){
if (max < num){
max = num;
} else if ( min > num){
min = num;
}
}
int gap = (max-min)/(len-1);
if( gap == 0){
gap = 1;
}
int size = (max - min) / gap + 1;
int[] gapMax = new int[size];
int[] gapMin = new int[size];
for (int num : nums){
int pos = (num - min)/gap;
if (gapMax[pos] < num){
gapMax[pos] = num;
}
if (gapMin[pos] == 0 || gapMin[pos] > num){
gapMin[pos] = num;
}
}
int start = min;
int end = gapMax[0];
int result = end - start;
for (int i = 0; i < size - 1; i++){
start = gapMax[i] == 0 ? start : gapMax[i];
end = gapMin[i+1];
if (result < (end - start)){
result = end - start;
}
}
if (gapMax[size - 1] == 0 && end - start > result){
result = end - start;
} else if (gapMax[size - 1] != 0 && end - gapMax[size - 1] > result){
result = end - gapMax[size - 1];
}
return result;
}
}

✡ leetcode 164. Maximum Gap 寻找最大相邻数字差 --------- java的更多相关文章

  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 求最大间距

    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

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

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

    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】Maximum Gap(hard)★

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

  9. 164. Maximum Gap (Array; sort)

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

随机推荐

  1. Navicat for mysql 破解

    想用navicat for mysql 连接mysql,发现只能试用30天,感觉挺不爽的,购买的话发现价格一千多,好贵的软件. 所以想要破解一下,网上试了一些方法不行,最后找到了一种方法可以的 破解工 ...

  2. javaSE之Object及hashcode等相关知识

    object: package javaBasic; public class TestObject { public static void main(String[] args) { // TOD ...

  3. zoj3416 Balanced Number

    链接 这题纠结了好久,刚开始想到的是正解,不过想到可能会出现一个数支点不唯一的情况,这样就多算了,其实是我想多了,一个数只有一个支点. 这样就好像想到了,枚举支点的位置,保存力矩的状态. dp[i][ ...

  4. Android之打log

    Android之打log 1.在代码中加上自己的log 2,模块编译mm -B或者./mk mm/mmm packages/apps/Contacts/ 3编译成功后install或者push生成的a ...

  5. 【转】selenium之 定位以及切换frame

    转载自:http://www.voidcn.com/blog/huilan_same/article/p-6155896.html 很多人在用selenium定位页面元素的时候会遇到定位不到的问题,明 ...

  6. jquery动画基础

    根据id改变字体大小的动画 <div class="speech">样式切换</div> <div id="switcher"&g ...

  7. sm30表维护做排序

    好吧,之前有人问过,因为代码太少就一直没发...今天给出来吧 众所周知,表维护其实就是个TC,只是表维护是统一的,没有使用通用名内表名什么的,这个就不多说了,来重点: TC的一般排序可以放在:1,PB ...

  8. apche的主配置文件)

    apche的主配置文件conf/httpd.conf(根据个人主机的路径设置,以下仅供参考) 需配置的行号与方法(示列): 172  #ServerName localhost:80 173 Serv ...

  9. 弄清UTF8和Unicode

    长期以来,一直对字符串编码认识比较粗略,认为支持"特殊字符"编码就是Unicode.当然,.NET平台上很少需要考虑这类问题,但搞清一些基本概念还是很有好处的. Unicode这个 ...

  10. Flume使用小结

    本文介绍初次使用Flume传输数据到MongoDB的过程,内容涉及环境部署和注意事项. 1 环境搭建 需要jdk.flume-ng.mongodb java driver.flume-ng-mongo ...