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.

Credits:
Special thanks to @porker2008 for adding this problem and creating all test cases.

 
 
如果不考虑题目中要求的Linear time/space,实际上只需要先sort,然后找到最大的就可以了。
 class Solution {
public:
int maximumGap(vector<int> &num) { if(num.size()<)
return ; sort(num.begin(),num.end());
int max=; for(int i=;i<num.size()-;i++)
{
if(num[i+]-num[i]>max)
max=num[i+]-num[i];
}
return max;
}
};
 
符合题意的方法:
 
由于num中的数字肯定在[min,max]区间内,所以根据抽屉原理,假设num中有n个数字,则最大的gap必然要大于dis=(max-min)/(n-1),所以我们可以把num所在的范围分成等间隔的区间,相邻区间内的元素之间的最大差值,即为要寻找的gap
 
 class Solution {
public:
int maximumGap(vector<int> &num) { if(num.size()<) return ;
if(num.size()==) return abs(num[]-num[]); int n=num.size();
int min,max,i; min=max=num[]; for(i=;i<num.size();i++)
{
if(min>num[i]) min=num[i];
if(max<num[i]) max=num[i];
} // 找到区间间隔
//注意此处,也可以写成(max-min)/n+1,此时就不需要num.size()==2的边界条件了
int dis=(max-min)/(n-)+; vector<vector<int> > bucket((max-min)/dis+); for(i=;i<n;i++)
{
int x=num[i];
int index=(x-min)/dis;
//把元素放入不同的区间中
if(bucket[index].empty())
{
bucket[index].reserve();
bucket[index].push_back(x);
bucket[index].push_back(x);
}
else
{
if(bucket[index][]>x) bucket[index][]=x;
if(bucket[index][]<x) bucket[index][]=x;
}
} int pre=;
int gap=; //在相邻的区间中(区间内有元素的相邻区间)寻找最大的gap
for(i=;i<bucket.size();i++)
{
if(bucket[i].empty()) continue; int tmp=bucket[i][]-bucket[pre][];
if(gap<tmp) gap=tmp;
pre=i;
}
return gap;
}
};

【leetcode】Maximum Gap的更多相关文章

  1. 【leetcode】Maximum Gap(hard)★

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

  2. 【leetcode】Maximum Subarray (53)

    1.   Maximum Subarray (#53) Find the contiguous subarray within an array (containing at least one nu ...

  3. 【leetcode】Maximum Subarray

    Maximum Subarray Find the contiguous subarray within an array (containing at least one number) which ...

  4. 【LeetCode】Maximum Product Subarray 求连续子数组使其乘积最大

    Add Date 2014-09-23 Maximum Product Subarray Find the contiguous subarray within an array (containin ...

  5. 【LeetCode】Maximum Depth of Binary Tree

    http://oj.leetcode.com/problems/maximum-depth-of-binary-tree/ public class Solution { public int max ...

  6. 【LeetCode】Maximum Depth of Binary Tree(二叉树的最大深度)

    这道题是LeetCode里的第104道题. 给出题目: 给定一个二叉树,找出其最大深度. 二叉树的深度为根节点到最远叶子节点的最长路径上的节点数. 说明: 叶子节点是指没有子节点的节点. 示例: 给定 ...

  7. 【LeetCode】Maximum Subarray(最大子序和)

    这道题是LeetCode里的第53道题. 题目描述: 给定一个整数数组 nums ,找到一个具有最大和的连续子数组(子数组最少包含一个元素),返回其最大和. 示例: 输入: [-2,1,-3,4,-1 ...

  8. 【leetcode】Maximum Product of Word Lengths

    Given a string array words, find the maximum value of length(word[i]) * length(word[j]) where the tw ...

  9. 【Leetcode】Maximum Product Subarray

    Find the contiguous subarray within an array (containing at least one number) which has the largest ...

随机推荐

  1. jexus防止产生 *.core文件

    1. jexus防止产生 *.core文件 # vi jws 新增: ulimit -c 0 >/dev/null 2>&1 2.删除*.core # sudo rm -rf *. ...

  2. hibernate4中使用Session doWork()方法进行jdbc操作(代码)

    Hibernate3.3.2版本中getSession().connection()已被弃用,hibernate4中官方推荐使用Session doWork()方法进行jdbc操作 首先看看Work接 ...

  3. 使用supervisor监控进程

    在linux下监控进程,可以使用inittab,最近找到了supervisor,也很好用,记录一下:1.系统要安装python,并安装与之对应的setuptools,下载地址在此2.安装:# sh s ...

  4. [转]Ubuntu 16.04建议安装

    Ubuntu 16.04发布了,带来了很多新特性,同样也依然带着很多不习惯的东西,所以装完系统后还要进行一系列的优化. 1.删除libreoffice libreoffice虽然是开源的,但是Java ...

  5. shell编程中的select用法

    select 语句 select表达式是bash的一种扩展应用,擅长于交互式场合.用户可以从一组不同的值中进行选择: select var in ... ; do break; done .... n ...

  6. acdream1233 Royal Federation (构造?)

    http://acdream.info/problem?pid=1233 Andrew Stankevich's Contest (3) ASC 3 Royal Federation Special ...

  7. 系统研究Airbnb开源项目airflow

    开源项目airflow的一点研究 调研了一些几个调度系统, airflow 更满意一些. 花了些时间写了这个博文, 这应该是国内技术圈中最早系统性研究airflow的文章了.  转载请注明出处 htt ...

  8. SQL 语句-partition by

    /****** ******/ 初始化数据 create table employee (empid int ,deptid int ,salary decimal(10,2)) insert int ...

  9. mybatis 添加事物后 无法获取自增主键的问题

    检查代码后没发现mapper文件设置自增主键返回的问题,后来检查到,关闭事务后,执行完是可以获取返回的主键的, 我在mysql的客户端里关闭自动提交,发现使用select last_insert_id ...

  10. gcc 4.8.3 install centos

    http://blog.csdn.net/xlx921027/article/details/17382643