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.
分析:
利用桶排序求解。首先,遍历数组nums[]求得min和max。假设数组共有N个数,则所求的解必然大于等于len=(max-min)/(N-1)(向上取整,且在N个数均匀分布时取得)。接着再遍历一次数组,将(nums[i]-min)/len作为下标,放入相应的桶中。其中,每个桶只需要维护该桶内的最大值和最小值即可。因为,每个桶里面所有的数最大不会相差len,所以桶内部是不存在解的,因此解只可能在相邻的桶中获得,即后一个桶的最小值减去前一个桶的最大值是可能的解。最后再从这些差值中取得一个最大值即可。
代码:
class Solution {
public:
int maximumGap(vector<int>& nums) {
if(nums.size()<2){
return 0;
}
int min=nums[0],max=nums[0];
for(int i=1;i<nums.size();i++){
min = nums[i]<min ? nums[i]:min;
max = nums[i]>max ? nums[i]:max;
}
if(min==max){ //数组中每个元素都相同
return 0;
}
int len;
if((max-min)%(nums.size()-1)==0){
len=(max-min)/(nums.size()-1);
}else{
len=(max-min)/(nums.size()-1)+1;
}
//每个桶只要保存最大值和最小值即可
int* bMin=new int[nums.size()];
int* bMax=new int[nums.size()];
for(int i=0;i<nums.size();i++){
bMax[i]=0x80000000;
}
for(int i=0;i<nums.size();i++){
int p=(nums[i]-min)/len;
if(bMax[p]==0x80000000){ //空桶直接插入
bMax[p]=bMin[p]=(nums[i]-min);
}else{
bMax[p] = bMax[p]>(nums[i]-min) ? bMax[p]:(nums[i]-min);
bMin[p] = bMin[p]<(nums[i]-min) ? bMin[p]:(nums[i]-min);
}
}
int pre=0;
int cur=1;
int ans=0x80000000;
while(cur<nums.size()){
while(cur<nums.size()&&bMax[cur]==0x80000000){//找到下一个不为空的桶
cur++;
}
if(cur==nums.size()){
break;
}
ans = ans>(bMin[cur]-bMax[pre]) ? ans:(bMin[cur]-bMax[pre]);
pre=cur;
cur++;
}
return ans;
}
};
LeetCode题解-----Maximum Gap的更多相关文章
- 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 题解]: Maximum Subarray
前言 [LeetCode 题解]系列传送门: http://www.cnblogs.com/double-win/category/573499.html 1.题目描述 Find the c ...
- 【leetcode】Maximum Gap(hard)★
Given an unsorted array, find the maximum difference between the successive elements in its sorted f ...
- ✡ leetcode 164. Maximum Gap 寻找最大相邻数字差 --------- java
Given an unsorted array, find the maximum difference between the successive elements in its sorted f ...
- [LeetCode 题解]: Maximum Depth of Binary Tree
Given a binary tree, find its maximum depth. The maximum depth is the number of nodes along the long ...
- [LeetCode] 164. Maximum Gap 求最大间距
Given an unsorted array, find the maximum difference between the successive elements in its sorted f ...
- Java for LeetCode 164 Maximum Gap
Given an unsorted array, find the maximum difference between the successive elements in its sorted f ...
随机推荐
- 为什么.NET感觉上比Java差一点
其实,我本人很喜欢.NET技术.工作经历中,大部分时间也在使用.NET开发. 这几年,由于工作的原因,开始进入Java+Linux世界. 今年,开始学习Python和Scala. 使用.NET时,有种 ...
- uml入门之14图与图之间的关系
1.先奉上整理的14图. 2.其次奉上整理的图之间的6种关系
- H5前端面试题及答案(1)
前几天去面试了一家公司,整下改公司的面试题. 1.新的 HTML5 文档类型和字符集是? HTML5 文档类型很简单: <!doctype html> HTML5 使用 UTF-8 编码示 ...
- JavaScript正则表达式小记
RegExp.html div.oembedall-githubrepos{border:1px solid #DDD;border-radius:4px;list-style-type:none;m ...
- jQuery淡入淡出效果轮播图
用JavaScript做了平滑切换的焦点轮播图之后,用jQuery写了个简单的淡入淡出的轮播图,代码没有做优化,html结构稍微有一些调整,图片部分用ul替换了之前用的div. html结构如下: & ...
- .NET的内存限制
之前做点云的.Net程序,经常因为数据量大出现Outofmemory异常,但是看看任务管理器,内存还有好多剩余的,在网上搜了一下发现这样的解释. 不管系统内存多大,目前一个.NET 对象最多只能够使用 ...
- java调用html模板发送html内容的邮件
在项目需要发送邮件,普通内容的邮件觉得太单调.太丑,没逼格,所以说直接把用到的邮件内容做成一个html模板,发送之前将对应参数替换掉,发送html内容的高逼格邮件. 首先需要引用jar包,这就不多说了 ...
- sublime text 3 安装
sublime text 3 下载地址 http://www.sublimetext.com/3 下载windows版本,然后解压缩就可以直接使用了,不错不错哦, 为了更加便捷的管理,安装 packa ...
- Android文件操作
将数据写入Internal Storage: String fileName = "myfile.txt"; String str="保存数据到内部存储"; t ...
- 【Android开发资料分享】自己整理的Android开发资料,非常全面
学习Android以来,不知不觉中收集了大量非常优秀的Android开发资料,一直没有系统的整理,最近抽时间把收藏夹中的资料做了一下整理,现在分享给大家,希望能够帮助到需要的人.这份资料我还会不断的更 ...