题目描述:

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的更多相关文章

  1. LeetCode 164. Maximum Gap[翻译]

    164. Maximum Gap 164. 最大间隔 Given an unsorted array, find the maximum difference between the successi ...

  2. 【leetcode】Maximum Gap

    Maximum Gap Given an unsorted array, find the maximum difference between the successive elements in ...

  3. leetcode[164] Maximum Gap

    梅西刚梅开二度,我也记一题. 在一个没排序的数组里,找出排序后的相邻数字的最大差值. 要求用线性时间和空间. 如果用nlgn的话,直接排序然后判断就可以了.so easy class Solution ...

  4. [LeetCode 题解]: Maximum Subarray

    前言   [LeetCode 题解]系列传送门:  http://www.cnblogs.com/double-win/category/573499.html   1.题目描述 Find the c ...

  5. 【leetcode】Maximum Gap(hard)★

    Given an unsorted array, find the maximum difference between the successive elements in its sorted f ...

  6. ✡ leetcode 164. Maximum Gap 寻找最大相邻数字差 --------- java

    Given an unsorted array, find the maximum difference between the successive elements in its sorted f ...

  7. [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 ...

  8. [LeetCode] 164. Maximum Gap 求最大间距

    Given an unsorted array, find the maximum difference between the successive elements in its sorted f ...

  9. Java for LeetCode 164 Maximum Gap

    Given an unsorted array, find the maximum difference between the successive elements in its sorted f ...

随机推荐

  1. edittext 监听内容变化

    给EditText追加ChangedListener可以监听EditText内容变化的监听 如图是效果图  类似于过滤的一种实现 1  布局也就是一个EditText,当EditText内容发生变化时 ...

  2. 如何安装nodejs

    1.进入官网https://nodejs.org/en/download/ 2.安装过程基本直接“NEXT”就可以了.(windows的安装msi文件在过程中会直接添加path的系统变量,变量值是你的 ...

  3. jquery fadeOut 异步

    1. 概述 jquery实现动画效果的函数使用起来很方便,不过动画执行是异步的, 所以要把自定义的操作放在回调函数里. 2. example <html> <body> < ...

  4. linux怎么模糊查找一个文件

    linux如何模糊查找一个文件 在当前目录下搜索指定文件: find . -name test.txt 在当前目录下模糊搜索文件: find . -name '*.txt' 在当前目录下搜索特定属性的 ...

  5. 开源论坛MvcForum推荐

    MvcForum算是Asp.net中开源论坛佼佼者之一.主要使用ASP.NET MVC 5 &Unity & Entity Framework 6,有较强的可撸性.是论坛开发者的不二之 ...

  6. mysql=null的优雅解决方法

    对于不是采用所有字段都是not null的mysql表设计而言,mysql提供了一个<=>操作符. 在oracle中我们的处理方式通常类似: where a = #{var} or #{v ...

  7. (四)play之yabe项目【页面】

    (四)play之yabe项目[页面] 博客分类: 框架@play framework   主页面 显示当前发表博客的完整内容,以及历史博客列表 Bootstrap Job 一个play job任务就是 ...

  8. 移动端调试工具-Debuggap

    随着移动互联网的迅速崛起,开发移动应用程序越来越多,但如果在移动端开发应用程序需要调试时,额- 仿佛又回到了IE时代,最方便也只能到处 alert 来调试.目前已经有一款产品可以做到这一点,比如pho ...

  9. JavaScript中的ParseInt("08")和“09”返回0的原因分析及解决办法

    今天在程序中出现一个bugger ,调试了好久,最后才发现,原来是这个问题. 做了一个实验: alert(parseInt("01")),当这个里面的值为01====>07时 ...

  10. 通过源码理解UST(用户栈回溯)

    UST原理:如果gflags标志中包含了UST标志,堆管理器会为当前进程分配一块内存,这个内存区域就是UST数据库(user-mode stack trace database),并建立一个STACK ...