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.

Solution:

http://cgm.cs.mcgill.ca/~godfried/teaching/dm-reading-assignments/Maximum-Gap-Problem.pdf

由于要求时间和空间复杂度是O(n), 因此用Java自带的sort就不行了(因为STL函数的sort()的复杂度是O(nlogn))

所以我们在思考得用一种线性的排序方法来做此题,那么,线性的排序算法有哪些呢?

Answer: 计数排序,基数排序,桶排序。

下面的code是利用桶排序来做的。

假设有N个元素A到B。

那么最大差值不会小于ceiling[(B - A) / (N - 1)],令bucket的大小len = ceiling[(B - A) / (N - 1)],初始化N-1个桶。

对于数组中的任意整数K(只针对数组中非min,max的值来放入桶中),很容易通过算式loc = (K - A) / len找出其桶的位置,然后维护每一个桶的最大值和最小值

由于同一个桶内的元素之间的差值至多为len - 1,因此最终答案不会从同一个桶中选择。

对于每一个非空的桶p,找出下一个非空的桶q,则q.min - p.max可能就是备选答案。返回所有这些可能值中的最大值。

public class Solution {
public int maximumGap(int[] num) {
if(num==null||num.length<2)
return 0;
if(num.length==2)
return num[0]>num[1]?num[0]-num[1]:num[1]-num[0];
int min=Integer.MAX_VALUE;
int max=Integer.MIN_VALUE;
for(int i:num){
min=Math.min(min, i);
max=Math.max(max, i);
} int gapRange=(int) Math.ceil((double)(max-min)/(num.length-1));
int[] bucketsMIN=new int[num.length-1];
int[] bucketsMAX=new int[num.length-1];
Arrays.fill(bucketsMIN, Integer.MAX_VALUE);
Arrays.fill(bucketsMAX, Integer.MIN_VALUE);
for(int i:num){
if(i==min||i==max)
continue;
int idx=(i-min)/gapRange;
bucketsMIN[idx]=Math.min(bucketsMIN[idx], i);
bucketsMAX[idx]=Math.max(bucketsMAX[idx], i);
}
int previous=min; int maxGap=Integer.MIN_VALUE;
for(int i=0;i<num.length-1;++i){
if(bucketsMAX[i]==Integer.MIN_VALUE&&bucketsMIN[i]==Integer.MAX_VALUE)
          //说明桶是空的  
continue;
maxGap=Math.max(maxGap, bucketsMIN[i]-previous);
previous=bucketsMAX[i];
}
maxGap=Math.max(maxGap, max-previous);
return maxGap;
}
}

[Leetcode] Maximum Gap的更多相关文章

  1. [LeetCode] Maximum Gap 求最大间距

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

  2. [LeetCode] Maximum Gap 解题思路

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

  3. 由Maximum Gap,对话桶排序,基数排序和统计排序

    一些非比较排序 在LeetCode中有个题目叫Maximum Gap.是求一个非排序的正数数列中按顺序排列后的最大间隔.这个题用桶排序和基数排序都能够实现.以下说一下桶排序.基数排序和计数排序这三种非 ...

  4. 【leetcode 桶排序】Maximum Gap

    1.题目 Given an unsorted array, find the maximum difference between the successive elements in its sor ...

  5. LeetCode 164. Maximum Gap[翻译]

    164. Maximum Gap 164. 最大间隔 Given an unsorted array, find the maximum difference between the successi ...

  6. 【leetcode】Maximum Gap

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

  7. leetcode[164] Maximum Gap

    梅西刚梅开二度,我也记一题. 在一个没排序的数组里,找出排序后的相邻数字的最大差值. 要求用线性时间和空间. 如果用nlgn的话,直接排序然后判断就可以了.so easy class Solution ...

  8. 【LeetCode】164. Maximum Gap (2 solutions)

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

  9. 【刷题-LeetCode】164 Maximum Gap

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

随机推荐

  1. 七牛---以一个七牛上传的实例小结下AJAX跨域【转】

    http://blog.csdn.net/netdxy/article/details/50699842 使用七牛过程中,很多用户或多或少遇到跨域的问题,这篇文章主要介绍下跨域的概念来看什么情况下会出 ...

  2. Delphi的面向对象编程基础笔记

    1.面向对象.一门面向对象的编程语言至少要实现以下三个OOP的概念 封装:把相关的数据和代码结合在一起,并隐藏细节.封装的好处是利用程序的模块化,并把代码和其他代码分开 继承:是指一个新的类能够从父类 ...

  3. 安装oracle 10g RAC执行的几个脚本说明

    1,/u01/app/oracle/oraInventory/orainstRoot.sh 脚本 #!/bin/sh if [ -d "/etc" ]; then /etc; fi ...

  4. POJ3321 Apple Tree(树状数组)

    先做一次dfs求得每个节点为根的子树在树状数组中编号的起始值和结束值,再树状数组做区间查询 与单点更新. #include<cstdio> #include<iostream> ...

  5. html5 svg 圆形进度条

    html5 svg 圆形进度条 <!DOCTYPE html> <html lang="en"> <head> <meta charset ...

  6. C 和 C++ 混合代码 cmath编译出错

    最近在网上下载了 Triangle 库,准备在程序中调用来三角化生成网格,但出现了很多错误,如下: 1>  triangle.c1>d:\program files\visualstudi ...

  7. 移动 Web 开发技巧

    1.使用click会出现绑定点击区域闪一下的情况,解决:给该元素一个样式如下 -webkit-tap-highlight-color: rgba(0,0,0,0); 2.用iphone或ipad浏览很 ...

  8. redis如何安装

    http://www.redis.net.cn/download/ (1)下载,解压,编译: $ wget http://download.redis.io/releases/redis-3.0.6. ...

  9. 使用nginx做负载均衡的session共享问题

    查了一些资料,看了一些别人写的文档,总结如下,实现nginx session的共享PHP服务器有多台,用nginx做负载均衡,这样同一个IP访问同一个页面会被分配到不同的服务器上,如果session不 ...

  10. Eclipse导出可执行Java工程/可执行Jar文件(包含第三方Jar包)

    1. 首先,右键你的Java工程,选择Export,在Java文件夹下选择Runnable JAR file,如下图所示: 2. 选择Runnable JAR file后,会弹出如下所示的对话框,选择 ...