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. Js综合笔记

    -----网页禁止复制---- -----网页禁止复制---- <body> <SCRIPT language=javascript type=text/javascript> ...

  2. Hibernate <一级缓存>

    Hibernate缓存分为三级: 一级缓存:基于事务级别(内存)的缓存,也可以成为session级别缓存 二级缓存:依赖于第三方,当请求一个对象时,先在缓存里面查找,如果没有就执行查询语句 查询缓存: ...

  3. Java注释@Override

    @Override指定方法覆载.它可以强制一个子类必须覆盖父类的方法. package ch14; /** * Created by Jiqing on 2016/12/27. */ public c ...

  4. Find a point on a 'line' between two Vector3

    Find a point on a 'line' between two Vector3http://forum.unity3d.com/threads/find-a-point-on-a-line- ...

  5. SPSS常用基础操作(1)——变量分组

    有时我们需要对数据资料按照某个规则进行归组,如 在上述资料中,想按照年龄进行分组,30岁以下为组1,30-40岁为组2,40岁以上为组3 有两种方法可以实现: 1.使用计算变量功能 <1> ...

  6. angularjs简述

    1.MVC设计模式 MVC 是一种使用 MVC(Model View Controller 模型-视图-控制器)设计创建 Web 应用程序的模式 Model(模型)表示应用程序核心(比如数据库记录列表 ...

  7. iOS开发拓展篇—音频处理(音乐播放器5)

    iOS开发拓展篇—音频处理(音乐播放器5) 实现效果: 一.半透明滑块的设置 /** *拖动滑块 */ - (IBAction)panSlider:(UIPanGestureRecognizer *) ...

  8. 怎么获取iOS的私有API

    前言 作为iOS开发人员,相信大多数伙伴都对怎么获取iOS的私有API很有兴趣,最近通过查找资料,总结了以下三种方法,希望对你有用. 第一种(class-dump) 不得不说这是一个很棒的工具,安装和 ...

  9. [最短路径SPFA] POJ 1847 Tram

    Tram Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 14630 Accepted: 5397 Description Tra ...

  10. 用maven搭建 testNG+PowerMock+Mockito测试框架

    单元测试是开发中必不可少的一部分,是产品代码的重要保证. Junit和testNG是当前最流行的测试框架,Junit是使用最广泛的测试框架,有兴趣的话自己baidu一下. testNG基于Junit和 ...