给定一个无序的数组,找出数组在排序后相邻的元素之间最大的差值。
尽量尝试在线性时间和空间复杂度情况下解决此问题。
若数组元素个数少于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. Spring中的IOC容器(学习笔记)

    如何将Bean配置到Spring的Bean容器中 通过xml配置文件: Bean实现类来自第三方类库:如“DataSource”等      需要命名空间配置如:context,aop,mvc等   ...

  2. python 目录下的__init__.py

    1 一个目录要成为一个package必须有__init__.py文件 The __init__.py files are required to make Python treat the direc ...

  3. VC实现趋势图绘制

    本文参考pudn上一个完整工程,在pudn搜索“50815867CurveDrawing”即可找到源代码.   上图是使用VS2010重写了该软件后的效果图,下面再贴出关键代码: // Plot.cp ...

  4. SDUT 3033 这题实在不知道起啥名好了(思维巧法)

    这题实在不知道起啥名好了 Time Limit: 1000ms   Memory limit: 65536K  有疑问?点这里^_^ 题目描述 懒得想背景故事了,开门见山. 有一个长度为n的整数数列A ...

  5. Navicat——如何导出所有的查询数据

    前言 很简单就是通过Navicat的查询来查询~ 步骤 真的不要太简单了~ 打开Navicat并点击查询 新建查询 选择对应的连接和库 写入SQL并运行 导出结果 1.选择导出当前的结果 2.选择保存 ...

  6. 前端如何展示商品属性:SKU多维属性状态判断算法的应用-Vue 实现

    由于公司开发了一个电商项目,涉及到前台商品属性的展示,所以百度上找了一下!找到了 周琪力写的一个算法例子,因为作者只有jQuery 实现demo, 自己仿照 demo 实现了一个 vue 的! 周琪力 ...

  7. golang copy函数

    数组切片内容复制 转自:http://studygolang.com/articles/4560 用于将内容从一个数组切片复制到另一个数组切片.如果加入的两个数组切片不一样大,就会按其中较小的那个数组 ...

  8. laravel打印原生语句

    laravel中快捷方便的打印语句的方法步骤一:DB::connection()->enableQueryLog();$data["banner"] = WebsiteBan ...

  9. Digit(湘潭大学比赛)

    题目链接: 点击打开链接 中文问题目就不解释了. 思路,找到这个数对应的的数字是多少,然后对这个数取对应的位置. 步骤:先打表打出一位数字对应字符串的长度,两位数的,到8,9就差不多了. 先确定给定的 ...

  10. BZOJ1040:骑士(基环树DP)

    Z国的骑士团是一个很有势力的组织,帮会中汇聚了来自各地的精英.他们劫富济贫,惩恶扬善,受到社会各界的赞扬.最近发生了一件可怕的事情,邪恶的Y国发动了一场针对Z国的侵略战争.战火绵延五百里,在和平环境中 ...