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. eclipse 条件断点的使用

    在某些特殊情况下,我们可能需要远程debug服务器进行问题追踪排查.比如在系统日志不够完善,没法定位问题的情况下需要远程debug进行排查. 但是服务器处于并发调用状态,怎样才能不影响其他业务系统调用 ...

  2. Oracle数据备份bat

    @echo on setlocal rem 设置数据库和备份文件参数... set sid=testorcl set username=testname set password=testpwd se ...

  3. sbrk与brk的使用小例子

    sbrk() 和 brk() - Unix的系统函数   sbrk()和brk() 系统的底层会维护一个位置,通过位置的移动完成内存的分配和回收.映射内存时 以一个内存页作为基本单位.   void* ...

  4. Linux 笔记总览

    LInux 性能分析 Linux IO实时监控命令详解

  5. myeclipse中disable maven nature

    1.直接原因:出现这个问题,一般都是因为手抖误操作. 但是出现了问题,还不知道从何查起. 可能出现的场景是eclipse安装Maven插件后,右键项目却找不到Maven按钮,继而无法编译项目. 2.实 ...

  6. 函数指针与指针函数以及typedef

    c难于理解的是指针,其魅力之处也是指针,函数方法结构,化繁为简可以理解为:返回值 函数名(形参表),具体来说: 返回值:1.可以为空void 2.基本数据类型char short int long f ...

  7. SPSS数据分析—多重线性回归

    只有一个自变量和因变量的线性回归称为简单线性回归,但是实际上,这样单纯的关系在现实世界中几乎不存在,万事万物都是互相联系的,一个问题的产生必定多种因素共同作用的结果. 对于有多个自变量和一个因变量的线 ...

  8. poj 2777

    题意:两个操作:c l r x   l到r之间的颜色变成x q l r      询问l到r有多少种颜色 思路:记一个整数表示哪种颜色是否取了 这里真的是煞笔了,看到这一题第一直觉是异或,但是A^A= ...

  9. .gitignore

    # Xcode # build/ *.pbxuser !default.pbxuser *.mode1v3 !default.mode1v3 *.mode2v3 !default.mode2v3 *. ...

  10. Python排序算法

    不觉已经有半年没写了,时间真是容易荒废,这半年过了个春节,去拉萨旅行.本职工作也很忙,没有开展系统的学习和总结. 今年开始静下心来从基础开始学习,主要分为三部分,算法.线性代数.概率统计. 首先学习算 ...