[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 ...
随机推荐
- 基于Bootstrap简单实用的tags标签插件
http://www.htmleaf.com/jQuery/ jQuery之家 自由分享jQuery.html5和css3的插件库 基于Bootstrap简单实用的tags标签插件
- [webkit移动开发笔记]之如何去除android上a标签产生的边框(转)
转载地址:http://www.cnblogs.com/PeunZhang/archive/2013/02/28/2907708.html 去年年底,做完最后一个项目就可以开开心心回家,可是在测试阶段 ...
- 【JAVA线程间通信技术】
之前的例子都是多个线程执行同一种任务,下面开始讨论多个线程执行不同任务的情况. 举个例子:有个仓库专门存储货物,有的货车专门将货物送往仓库,有的货车则专门将货物拉出仓库,这两种货车的任务不同,而且为了 ...
- git 创建本地分支,然后推送到服务器上
git checkout -b crm-2.repair-callback.phoneSet git checkout -b crm-2.repair-callback.RepairHis git p ...
- WPF中使用ReportViewer报表
本篇博客将介绍如何在WPF中使用ReportViewer控件. 1. 环境准备:下载安装最新版ReportViewer(PS:需要安装Microsoft SQL Server System CLR T ...
- jquery mobile 转场闪屏的解决
jqm转场闪屏是用phonegap生成apk非常容易遇到的问题,暂时貌似还是没有完美的解决方案,网上暂时有一些方案,个人都尝试了一下发现还是改背景比较有效,总结如下: 改变默认css文件: .ui- ...
- hdu 4739 2013杭州赛区网络赛 寻找平行坐标轴的四边形 **
是平行坐标轴的,排个序搞一下就行了,卧槽,水的不行 如果不是平行的,则需要按照边长来判断
- Windows phone 8.0 本地化遇到的两个问题
基本上来说,按照msdn来讲的,本地化和全球化没有太多的问题,链接如下: http://msdn.microsoft.com/zh-cn/library/windowsphone/develop/ff ...
- 程序员最喜爱的12个Android应用开发框架二(转)
在上一篇程序员最喜爱的12个Android应用开发框架(一)中,我们为大家介绍了前6个Android应用开发框架,主要包括了 Xamarin.Phonegap.Corona SDK等.接下来,小编将继 ...
- dwz中权限的控制
很多人不明白用dwz要如何在没有登录的时候跳转到登录页面,没有权限的时候弹出提示. 其实,作者在设计的时候,已经完全考虑到了这些需求. 不管是navTab还是dialog,dwz的页面加载最终都是通过 ...