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.

思路:

这是一道难题,难点在于不能排序,但是要找到在排序的情况下相邻数字的最大差。

各种想不出来,很low的用了排序,居然也通过了。

然后看答案,发现可以很巧妙的利用桶的思想。找到数组里面的最大值和最小值,则数字间隔gap满足:

gap >= (max - min) / (N - 1)  结果向下取整  注意gap = 0 时取 1, 防止后面除0

然后,可以分为 (max - min) / gap + 1 个桶

每个数字根据  (num[i] - min) / gap 来决定放在哪个桶里。

记录每个桶里的最大值和最小值。

则最大间隔不会在桶内,而会在相邻的桶之间,bucket[i].min - bucket[i - 1].max 中最大的数字就是目标数字。

//这个虽然通过了,但是用了排序,是O(nlogn)的算法
int maximumGap(vector<int> &num) {
int ans = ;
sort(num.begin(), num.end());
for(int i = ; i < num.size(); i++)
{
ans = num[i] - num[i - ];
}
return ans;
}
---------------------------------------------------------------------------------
//答案的思路
int maximumGap1(vector<int> &num) {
if(num.size() < ) return ;
//第一步,找最大和最小值
int maxnum = num[];
int minnum = num[];
for(int i = ; i < num.size(); i++)
{
maxnum = (maxnum < num[i]) ? num[i] : maxnum;
minnum = (minnum > num[i]) ? num[i] : minnum;
}
//第二步:判断间隔的大小
int gap = (maxnum - minnum) / (num.size() - );
gap = (gap == ) ? : gap;
//判断和记录每个桶的最大和最小数字
vector<vector<int>> bucket((maxnum - minnum) / gap + , vector<int>(, -)); //只需记录每个桶的最大和最小数字
for(int i = ; i < num.size(); i++)
{
int belong = (num[i] - minnum) / gap;
bucket[belong][] = (num[i] > bucket[belong][]) ? num[i] : bucket[belong][]; //更新最大值
bucket[belong][] = (bucket[belong][] < || num[i] < bucket[belong][]) ? num[i] : bucket[belong][]; //更新最小值
} //找最大间隔
int ans = ;
int pre = ;
for(int i = ; i < bucket.size(); i++)
{
if(bucket[i][] == -) continue;
ans = (bucket[i][] - bucket[pre][] > ans) ? bucket[i][] - bucket[pre][] : ans;
pre = i;
}
return ans;
}

网上还有一种方法是利用基数排序,我还没看。

int maximumGap(std::vector<int> &num) {
for(unsigned bit = ; bit < ; bit++)
std::stable_partition(num.begin(), num.end(), [bit](int a){
return !(a & ( << bit));
});
int difference = ;
for(std::size_t i = ; i < num.size(); i++) {
difference = std::max(difference, num[i] - num[i-]);
}
return difference;
}

【leetcode】Maximum Gap(hard)★的更多相关文章

  1. 【leetcode】Maximum Gap

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

  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. nginx TCP 代理& windows傻瓜式安装

    一.下载nginx Windows http://nginx.org/en/download.html 二.解压到目录 三.进入目录并start nginx.exe即可启动 cd d:/java/ng ...

  2. 日常使用的shell脚本

    1.shell实现无密码登陆 host=$ expect << EOF         spawn ssh-copy-id $host         expect "passw ...

  3. PHP截取中文无乱码函数——cutstr

    转载:http://blog.sina.com.cn/s/blog_694c144f010179wj.html 真正好用的PHP截取中文无乱码函数——cutstr (2012-07-09 11:17: ...

  4. Elven Postman(BST )

    Elven Postman Time Limit: 1500/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)T ...

  5. 元素间距属性(scrollLeft,scrollWidth,clientWidth,offsetWidth,padding,margin)

    scrollHeight: 获取对象的滚动高度.scrollLeft:设置或获取位于对象左边界和窗口中目前可见内容的最左端之间的距离scrollTop:设置或获取位于对象最顶端和窗口中可见内容的最顶端 ...

  6. C语言的源程序字符集与执行字符集

    我们程序文件的字符集就是我们写出来的.c扩展名的文件的字符集,这里用的是系统默认的 ANSI 字符集,如下图: 上面的字符集我们不关心,我们关心的是 源程序的字符集 和程序的 执行字符集 ,源程序的字 ...

  7. String、StringBuffer、StringBuilder的不同使用场景

    String 字符串常量StringBuffer 字符串变量(线程安全)StringBuilder 字符串变量(非线程安全) 简要的说, String 类型和 StringBuffer 类型的主要性能 ...

  8. MySQL中的运算符

    一.算法运算符: (A) 除法运算和模运算中,如果除数为0,非法,返回结果为NULL. 二.比较运算符: (A) 比较结果不确定是返回NULL. (B) 比较运算符可以用于比较数字.字符串和表达式.数 ...

  9. CSU 1503 点到圆弧的距离(2014湖南省程序设计竞赛A题)

    题目链接:http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1503 解题报告:分两种情况就可以了,第一种是那个点跟圆心的连线在那段扇形的圆弧范围内,这 ...

  10. Oracle 数据库1046事件

    例子: session 2: SQL> connect test/test Connected. select * from v$mystat where rownum=1; 143 selec ...