给定一个无序的数组,找出数组在排序后相邻的元素之间最大的差值。
尽量尝试在线性时间和空间复杂度情况下解决此问题。
若数组元素个数少于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. ZeroMQ 初步认识

    http://www.danieleteti.it/zeromq-for-delphi/ https://my.oschina.net/zeroflamy/blog/109457 http://zer ...

  2. boogo08---中间件

    package main //中间件1:只允许特定host请求过来 import ( "fmt" "net/http" ) //SingleHost是一个中间件 ...

  3. Android 源码架构

    我们都知道Android系统是一个开源工程,在网上可以下载到源代码. 一般在网上搜索一下,就会找到各种下载源代码的方法,比如使用Git和Repo,android源代码下载的网址是http://andr ...

  4. POJ 1861 Network (Kruskal算法+输出的最小生成树里最长的边==最后加入生成树的边权 *【模板】)

    Network Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 14021   Accepted: 5484   Specia ...

  5. FZU1686 神龙的难题 —— Dancing Links 可重复覆盖

    题目链接:https://vjudge.net/problem/FZU-1686 Problem 1686 神龙的难题 Accept: 812    Submit: 2394 Time Limit: ...

  6. string interpolation in sql server

    https://sqlserver.dev129.com/2018/01/29/string-interpolation-in-t-sql/ Most programming languages ha ...

  7. Collections工具类、Map集合、HashMap、Hashtable(十八)

    1.Map集合概述和特点 * A:Map接口概述 * 去重复, * 查看API可以知道, * 将键映射到值的对象, * 一个映射不能包含重复的键, * 每个键最多只能映射到一个值.* B:Map接口和 ...

  8. SPOJ:Lexicographically Smallest(并查集&排序)

    Taplu and Abhishar loved playing scrabble. One day they thought of inventing a new game using alphab ...

  9. 【前端】Element-UI 省市县级联选择器 JSON数据

    转载请注明出处:http://www.cnblogs.com/shamoyuu/p/element_cascader.html 不想自己处理的就直接下载吧 http://shamoyuu.bj.bce ...

  10. Android之APP模块编译

    一,如何把app编进系统 a.源码编译,在packages/apps目录下有安卓原生的app,以Bluetooth为例,源码根目录下有Android.mk文件: packages\apps\Bluet ...