给定一个无序的数组,找出数组在排序后相邻的元素之间最大的差值。
尽量尝试在线性时间和空间复杂度情况下解决此问题。
若数组元素个数少于2,则返回0。
假定所有的元素都是非负整数且范围在32位有符号整数范围内。

详见:https://leetcode.com/problems/maximum-gap/description/

Java实现:题目要求是要线性的时间和空间,那么只能用桶排序。同一个桶里的数差值不会有不同桶间的差值大,所以找桶内最大和下一个非空桶的桶内最小进行比较即可。

class Solution {
public int maximumGap(int[] nums) {
int n=nums.length;
if (nums == null || n < 2){
return 0;
}
// 计算数组中的最大值和最小值
int min = nums[0];
int max = nums[0];
for (int num:nums) {
min = min>num?num:min;
max = max<num?num:max;
}
// the minimum possibale gap, ceiling of the integer division
int gap = (int)Math.ceil((double)(max - min)/(n - 1));
int[] bucketsMIN = new int[n - 1]; // 记录桶中的最小值
int[] bucketsMAX = new int[n - 1]; // 记录桶中最大值
Arrays.fill(bucketsMIN, Integer.MAX_VALUE);
Arrays.fill(bucketsMAX, Integer.MIN_VALUE);
// put numbers into buckets
for (int num:nums) {
if (num == min || num == max){
continue;
}
int idx = (num - min) / gap; // index of the right position in the buckets
bucketsMIN[idx] = Math.min(num, bucketsMIN[idx]);
bucketsMAX[idx] = Math.max(num, bucketsMAX[idx]);
}
// scan the buckets for the max gap
int maxGap = Integer.MIN_VALUE;
int previous = min;
for (int i = 0; i < n - 1; i++) {
if (bucketsMIN[i] == Integer.MAX_VALUE && bucketsMAX[i] == Integer.MIN_VALUE){
// 跳过空桶
continue;
}
// min value minus the previous value is the current gap
maxGap = Math.max(maxGap, bucketsMIN[i] - previous);
// update previous bucket value
previous = bucketsMAX[i];
}
maxGap = Math.max(maxGap, max - previous); // 更新最大间隔
return maxGap;
}
}

参考:https://www.cnblogs.com/grandyang/p/4234970.html

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 (2 solutions)

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

  4. 【刷题-LeetCode】164 Maximum Gap

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

  5. [LeetCode] 164. Maximum Gap 求最大间距

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

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

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

  7. 164. Maximum Gap (Array; sort)

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

  8. Java for LeetCode 164 Maximum Gap

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

  9. 164. Maximum Gap *HARD* -- 无序数组找出排序后连续元素的最大间隔

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

随机推荐

  1. code[VS] 1297 硬币

    题目描写叙述 Description 我们知道即使是同一种面值的硬币,它们的重量也有可能不一样,由于它受到很多因素的影响,包含制造工艺和流程上的.可是不论什么一种面值的硬币的重量总是处于某个特定范围之 ...

  2. Hihocoder #1098 : 最小生成树二·Kruskal算法 ( *【模板】 )

    #1098 : 最小生成树二·Kruscal算法 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 随着小Hi拥有城市数目的增加,在之间所使用的Prim算法已经无法继续使用 ...

  3. 关于eclipse的resource文件没有发布到tomcat上的解决方案

    版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.csdn.net/luman1991/article/details/53457302

  4. YTU 2455: Pefect 数字

    2455: Pefect 数字 时间限制: 1 Sec  内存限制: 128 MB 提交: 749  解决: 146 题目描述 小明和小林做数字游戏,他们的游戏规则如下: 小明说出一个数字n,小林说出 ...

  5. Objective-C 中Socket常用转换机制(NSData,NSString,int,Uint8,Uint16,Uint32,byte[])

    最近项目中要用到socket通讯,由于涉及到组包问题,所以需要数据类型之间的来回转换,现在分享出来 如果想要请教Socket的问题请留言,我会随时回答的 1. int类型转16进制hexstring ...

  6. 【CQ18阶梯赛第一场】题解

    [A-风格不统一如何写程序] 输入字符串,得到长度,对于每个字符:如果是大写,则改为:‘_’+小写:如果是‘_’则忽略‘_’,并且把后面的小写改为大写. #include<cstdio> ...

  7. JS 之正则表达式

    字符 含意 \ 做为转意,即通常在"\"后面的字符不按原来意义解释,如/b/匹配字符"b",当b前面加了反斜杆后/\b/,转意为匹配一个单词的边界. -或- 对 ...

  8. 在线制作一键生成微信小程序实现原理之需求分析

    随着微信小程序接口不断的放开,小程序在今年或许是明年必将成为商家的一个标配,这个标配的标准就是要开发周期短,费用低,功能实用.只有这样才能让线下的广大商家快速接入.现在也有好多公司开发出了一键生成快速 ...

  9. 2、HTML的head内标签

    一.Meta(metadata information) 提供有关页面的元信息,例:页面编码.刷新.跳转.针对搜索引擎和更新频度的描述和关键词 1.页面编码(告诉浏览器是什么编码) <meta ...

  10. U3D 的一些基础优化

    1.在使用数组或ArrayList对象时应当注意 [csharp] view plaincopy length=myArray.Length; for(int i=0;i<length;i++) ...