[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 ...
随机推荐
- LINQ To DataSet 示例
如果在项目遇到这样的问题如:DataTable1和DataTable2需要根据一定的规则进行合并成一个DataTable3. 问题1:DataTable1不是读数据库表的结果,而是合成的数据集,因此无 ...
- golang channel buffer
package mainimport ( "fmt" "time")func main() { // Case-1: no buffer //chanMessa ...
- 关于WCF的一些注意事项
1.服务代理,建立通道的方法,要注意及时关掉代理,因为服务设置有一个服务的最大连接数,超过这个连接数,则后面的连接将会等待,一直到超时,报错!! 2.在已有配置的基础上,利用代码更改终结点,如果重设了 ...
- Session 类
Session 类 Session 类可以使用户在浏览您的网站时,维持他们的状态并跟踪他们的行为. Session 类将每个用户的 session 信息序列化(serialize)后存储到到 coo ...
- 解决linux下unzip中文有乱码的问题
xxx.zip 中有中文的文件,在linux下unzip就会有乱码. 解决办法:安装7zip 去http://sourceforge.net/projects/p7zip/files/latest/d ...
- 第二十篇:在SOUI中使用分层窗口
从Windows 2K开始,MS为UI开发引入了分层窗口这一窗口风格.使用分层窗口,应用程序的主窗口可以是半透明,也可以是逐点半透明(即每一个像素点的透明度可以不同). 可以说,正是因为有了分层窗口, ...
- PAT A 1014. Waiting in Line (30)【队列模拟】
题目:https://www.patest.cn/contests/pat-a-practise/1014 思路: 直接模拟类的题. 线内的各个窗口各为一个队,线外的为一个,按时间模拟出队.入队. 注 ...
- Educational Codeforces Round 3 E. Minimum spanning tree for each edge LCA/(树链剖分+数据结构) + MST
E. Minimum spanning tree for each edge Connected undirected weighted graph without self-loops and ...
- ios 音乐播放
#import "ViewController.h" #import <AVFoundation/AVFoundation.h> @interface ViewCont ...
- 汇编学习(六)——代码转换程序
(一)逻辑运算指令 一.双操作数逻辑运算指令 1.指令格式: AND dst,src ; "与"运算, OR dst,src ; "或"运算 XOR dst,s ...