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 ...
随机推荐
- Maven创建webapp(二)
这一节将记录在myeclipse下用maven创建一个简单的webapp项目 web开发maven仓库自动添加组件,故需要需要保持网络的通畅. 打开myeclipse --> File - ...
- ahjesus不断完善的js小"内裤"
String.prototype.isNullOrWhiteSpace = function () { /// <summary> /// 变量是undefined或者null或者空字符串 ...
- Linux下的crontab命令使用特别须注意的地方
1.如果命令中涉及到了年月日(如:date +%C%y%m%d),其中%必须进行转义,如下: date +\%C\%y\%m\%d 2.使用到的命令必须使用完整的路径: * * * /home/yxf ...
- Android应用开发基础之九:内容提供者(ContentProvider)
内容提供者 应用的数据库是不允许其他应用访问的 内容提供者的作用:就是让别的应用访问到你的数据库 自定义内容提供者,继承ContentProvider类,重写增删改查方法,在方法中写增删改查数据库的代 ...
- C# 循环语句 for循环(嵌套 while 穷举 迭代)
for循环的嵌套类似于if else 事例: 打印矩阵,外循环对应行,内循环对应列 for (int k = 1; k <= 5; k++) { for (int i = 1; i <= ...
- 微信公共平台开发2 .net
成功的走出第一步后,我们紧接着趁热打铁开始下一步: 成为了开发者之后微信平台会给您AppId和AppSecret,在订阅号中是没有的,所以因该申请一下服务号, 若没有请注意上一篇http://www. ...
- SharePoint 禁用本地回环的两个方法
有两种方法中,若要变通解决此问题,请根据您的具体情况使用下列方法之一. 方法 1: 指定主机名 (如果需要 NTLM 身份验证,请首选方法) 指定的主机名的映射到环回地址,并可以连接到 Web 站点在 ...
- vsphere vcenter server下安装ubuntu的vmwaretools
0.参考文献 百度经验:这里面是以redhat桌面版为实例进行介绍的,我的环境是ubuntu-server,虽然不一样,也可以参考 http://jingyan.baidu.com/article/2 ...
- Android系统提供的开发常用的包名及作用
android.app :提供高层的程序模型.提供基本的运行环境 android.content :包含各种的对设备上的数据进行访问和发布的类 android.database :通过内容提供者浏览和 ...
- C安全问题与指针误用
欢迎关注我的个人博客:www.wuyudong.com, 更多精彩文章与您分享 指针的声明与初始化 1.不恰当的指针声明 考虑如下的声明: int* ptr1, ptr2; // ptr1为指针,pt ...