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 ...
随机推荐
- python 的import机制2
http://blog.csdn.net/sirodeng/article/details/17095591 python 的import机制,以备忘: python中,每个py文件被称之为模块, ...
- 用linux遇到的一个死循环
1. 公司的服务器centos,需要通过vpn拨上去: 2. 然后ftp启用了tls加密: 3. 然后ubuntu 12.04 上libgnutls的版本比较新,装的filezilla 3.5.3,怎 ...
- SharePoint 2013 App Remote Event Receivers
当我们在使用App的时候,就会发现一些问题,比如那些网站部署.更新或者卸载了,我们很关心我们的App是否有人用,这就需要远程事件接收器了. 1.在我们的测试App的解决方案上选中鼠标,按F4弹出属性, ...
- 在 SharePoint Server 2013 中配置建议和使用率事件类型
http://technet.microsoft.com/zh-cn/library/jj715889.aspx 适用于: SharePoint Server 2013 利用使用事件,您可以跟踪用户与 ...
- 转:EClipse 10个最有用的快捷键
Eclipse快捷键 10个最有用的快捷键 Eclipse中10个最有用的快捷键组合 一个Eclipse骨灰级开发者总结了他认为最有用但又不太为人所知的快捷键组合.通过这些组合可以更加容易的浏览源代 ...
- 通过重写OnScrollListener来监听RecyclerView是否滑动到底部
为了增加复用性和灵活性,我们还是定义一个接口来做监听滚动到底部的回调,这样你就可以把它用在listview,scrollView中去. OnBottomListener package kale.co ...
- android 打包错误
打包时报如下错误: Export aborted because fatal lint errors were found. These are listed in the Lint View. Ei ...
- android 类ios actionsheet效果
1.http://blog.csdn.net/zhaoxy_thu/article/details/17733389 2. https://github.com/ojhariddhish/action ...
- IOS Quartz2D简介
Quartz2D 简介( 后续会有相关应用) 第一部分 绘制直线 代码示例: - (void)drawRect:(CGRect)rect{ //获取图形上下文 CGContextRef cxConte ...
- 【转】C++的拷贝构造函数深度解读,值得一看
建议看原帖 地址:http://blog.csdn.net/lwbeyond/article/details/6202256 一. 什么是拷贝构造函数 首先对于普通类型的对象来说,它们之间的复制是很 ...