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 ...
随机推荐
- ahjesus js 快速求幂
/* 快速幂计算,传统计算方式如果幂次是100就要循环100遍求值 快速幂计算只需要循环7次即可 求x的y次方 x^y可以做如下分解 把y转换为2进制,设第n位的值为i,计算第n位的权为x^(2^(n ...
- ButterKnife
1.简介 ButterKnife是注解中相对简单易懂的很不错的开源框架 1.强大的View绑定和Click事件处理功能,简化代码,提升开发效率 2.方便的处理Adapter里的ViewHolder绑定 ...
- IPC机制--Binder
文章来自 Android技术内幕 系统卷 转:http://www.linuxidc.com/Linux/2011-08/40508.htm 什么是IPC机制以及IPC机制的种类 在Linux中,是以 ...
- [android] 短信发送器
/*****************2016年4月23日 更新********************************/ 知乎:什么是 7 位元的字符? 英文字符难道不是 8 bit 是一个字 ...
- 升级sp1后文档无法编辑
现象: A problem occurred while connecting to the server. If the problem continues, contact your admini ...
- Docker: 解决Centos 7中Permission Denied的问题
当用docker -v挂载volume后,会出现Permission Denied的问题,这有时是因为SeLinux导致的.解决方法如下: chcon -Rt svirt_sandbox_file_t ...
- Swing(一):JFrame框架窗体
Swing窗体是一个组件,也是可视化的窗体,可以将其他组件放在这里.Jfream框架是一个容器,是Swing程序中各个组件的载体,可以将它看做为 一个容器,在开发中可以通过java.swing.jfr ...
- OC中的内存问题
常见的内存问题有以下几种: 1.野指针异常:访问没有所有权的内存,如果想要安全的访问,必须确保空间还在 2.内存泄露:空间使用完之后没有及时释放 3.过度释放:对同一块存储空间释放多次,立刻crash ...
- Unity与Android的相互交互
1.Unity调用Android. Unity块代码: using (AndroidJavaClass jc = new AndroidJavaClass("com.unity3d.play ...
- clang: error: no such file or directory: xxx.pch
今天打开一个下载的例子 报clang: error: no such file or directory: xxx.pch的错 说一下解决方案 1.先在你的工程里找到这.pch文件- 2.把它现在的路 ...