[Leetcode] Maximum Gap
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的更多相关文章
- [LeetCode] Maximum Gap 求最大间距
Given an unsorted array, find the maximum difference between the successive elements in its sorted f ...
- [LeetCode] Maximum Gap 解题思路
Given an unsorted array, find the maximum difference between the successive elements in its sorted f ...
- 由Maximum Gap,对话桶排序,基数排序和统计排序
一些非比较排序 在LeetCode中有个题目叫Maximum Gap.是求一个非排序的正数数列中按顺序排列后的最大间隔.这个题用桶排序和基数排序都能够实现.以下说一下桶排序.基数排序和计数排序这三种非 ...
- 【leetcode 桶排序】Maximum Gap
1.题目 Given an unsorted array, find the maximum difference between the successive elements in its sor ...
- LeetCode 164. Maximum Gap[翻译]
164. Maximum Gap 164. 最大间隔 Given an unsorted array, find the maximum difference between the successi ...
- 【leetcode】Maximum Gap
Maximum Gap Given an unsorted array, find the maximum difference between the successive elements in ...
- leetcode[164] Maximum Gap
梅西刚梅开二度,我也记一题. 在一个没排序的数组里,找出排序后的相邻数字的最大差值. 要求用线性时间和空间. 如果用nlgn的话,直接排序然后判断就可以了.so easy class Solution ...
- 【LeetCode】164. Maximum Gap (2 solutions)
Maximum Gap Given an unsorted array, find the maximum difference between the successive elements in ...
- 【刷题-LeetCode】164 Maximum Gap
Maximum Gap Given an unsorted array, find the maximum difference between the successive elements in ...
随机推荐
- Android屏幕旋转总结
转自:http://www.myexception.cn/operating-system/1452058.html 1. ProjectConifg.mk中定义宏MTK_LCM_PHYSICAL_R ...
- HTML CSS微信CSS显示一些总结
微信显示网页是调用腾讯自带的浏览器内核,由于腾讯浏览器内核对css展示效果没有谷歌浏览器好,导致用谷歌浏览器写好的网页,放到微信页面之后,显示的效果就发生变化,所以调整css样式显得那么吃力: 1. ...
- hdu 2393:Higher Math(计算几何,水题)
Higher Math Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total ...
- CentOS版本选择说明
官方下载站http://www.centos.org/download/ 所有版本下载地址http://vault.centos.org/ 首先对一些镜像文件做个简单的介绍: LiveCD一般用来修复 ...
- foreach与Iterable学习
以前对于foreach的使用都是自然而然的感觉,没有深究过为什么可以用,什么时候可以用.最近才发现,原来那些可以使用的类,都是实现了Iterable接口的,否则根本就不能用. 下面是我之前学习时候写的 ...
- 在source insight中集成astyle
转自:http://www.cnblogs.com/xuxm2007/archive/2013/04/06/3002390.html 好吧,我有代码格式的强迫症,代码不整齐,我看的都头疼,之前一直喜欢 ...
- Java学习随笔3:遍历文件夹及文件的读取和写入
import java.io.File; /** * 遍历文件夹 */ public class ScannerFile { public static void main(String[] args ...
- windows mysql 自动备份的几种方法
转自:http://www.cnblogs.com/liongis/archive/2013/03/12/2956573.html 1.复制date文件夹备份===================== ...
- Problem list
不定时更新,发现好题目但是没时间写的就添加,写完就删除. hdu5732 求树的重心 poj1741
- AngularJS学习之输入验证
1.AngularJS可以验证表单和控件可以验证输入的数据: 2.输入验证:客户端不能确保用户输入数据的安全,所以服务器端的数据验证也是必须的: 3.应用实例: <! DOCTYPE html& ...