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.

解法一:

先排序O(nlogn),再一次遍历,得到maxGap

虽然不满足O(n)的时间要求,但是最直观的想法。

class Solution {
public:
int maximumGap(vector<int>& nums) {
if(nums.empty() || nums.size() == )
return ;
sort(nums.begin(), nums.end());
int ret = ;
for(int i = ; i < nums.size(); i ++)
ret = max(ret, nums[i]-nums[i-]);
return ret;
}
};

解法二:为了满足O(n)复杂度,我尝试了计数排序,但是会TLE。因此使用桶排序来做。

(计数排序可以看做是桶大小为1的桶排序,但由于桶数目太多导致遍历时间过长。)

最大gap肯定是出现在后一个有效桶的min与前一个有效桶的max之间。

“有效”指的是忽略空桶。

class Solution {
public:
int maximumGap(vector<int>& nums) {
if(nums.empty() || nums.size() == )
return ;
int n = nums.size();
int minAll = *min_element(nums.begin(), nums.end());
int maxAll = *max_element(nums.begin(), nums.end());
// type conversion!!!
double gap = ((double)(maxAll - minAll)) / (n - );
// compute min and max element for each bucket
vector<int> minV(n-, INT_MAX);
vector<int> maxV(n-, INT_MIN);
for(int i = ; i < n; i ++)
{
if(nums[i] != maxAll)
{// the bktId of maxAll will fall out of bucket range
int bktId = (int)((nums[i]-minAll)/gap);
minV[bktId] = min(minV[bktId], nums[i]);
maxV[bktId] = max(maxV[bktId], nums[i]);
}
}
int ret = ;
int curMax = maxV[];
for(int i = ; i < n-; i ++)
{
if(minV[i] != INT_MAX)
{
ret = max(ret, minV[i]-curMax);
curMax = maxV[i];
}
}
ret = max(ret, maxAll-curMax);
return ret;
}
};

【LeetCode】164. Maximum Gap (2 solutions)的更多相关文章

  1. 【Leetcode】164. Maximum Gap 【基数排序】

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

  2. 【刷题-LeetCode】164 Maximum Gap

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

  3. 【LeetCode】53. Maximum Subarray (2 solutions)

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

  4. 【leetcode】998. Maximum Binary Tree II

    题目如下: We are given the root node of a maximum tree: a tree where every node has a value greater than ...

  5. 【LeetCode】895. Maximum Frequency Stack 解题报告(Python)

    [LeetCode]895. Maximum Frequency Stack 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxueming ...

  6. 【LeetCode】718. Maximum Length of Repeated Subarray 解题报告(Python)

    [LeetCode]718. Maximum Length of Repeated Subarray 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxu ...

  7. 【LeetCode】662. Maximum Width of Binary Tree 解题报告(Python)

    [LeetCode]662. Maximum Width of Binary Tree 解题报告(Python) 标签(空格分隔): LeetCode 题目地址:https://leetcode.co ...

  8. 【LeetCode】104. Maximum Depth of Binary Tree (2 solutions)

    Maximum Depth of Binary Tree  Given a binary tree, find its maximum depth. The maximum depth is the ...

  9. 【LeetCode】75. Sort Colors (3 solutions)

    Sort Colors Given an array with n objects colored red, white or blue, sort them so that objects of t ...

随机推荐

  1. 【BZOJ】【1941】【SDOI2010】Hide and Seek

    KD-Tree 一开始看错题了

  2. 在Spark上运行WordCount程序

    1.编写程序代码如下: Wordcount.scala package Wordcount import org.apache.spark.SparkConf import org.apache.sp ...

  3. json的好处-新一代数据传输利器

    JSON是一种轻量级的数据交换格式!和xml一样. 为什么不XML XML的冗余太大,不过XML阅读起来比较方面,所以并没有被json完全取代,很多时候都是并存.比如sina微博的开发平台有一个JSO ...

  4. OpenCV教程(41) 人脸特征检测

          在OpenCV中,自带着Harr分类器人脸特征训练的文件,利用这些文件,我们可以很方面的进行人脸,眼睛,鼻子,表情等的检测.      人脸特征文件目录: ../opencv2.46/op ...

  5. 解决LayoutItem lable 太长的问题

    <Style TargetType="dxlc:LayoutItem"> <Setter Property="AddColonToLabel" ...

  6. [CSS3]Clearfix

    <!DOCTYPE html> <html> <head> <link rel="stylesheet" href="index ...

  7. “/”应用程序中的server错误

    前几天敲ASP.NET有关验证控件的样例的时候,出现了这个问题: 可是对于这个问题我们应该都不陌生,之前敲牛腩的时候也出现过,当时的解决方法是: 在web.config中找         <a ...

  8. Asp.Net 之 前台绑定常用总结

    1.<%= 变量名 %> 里面放的是后台定义的变量名,如: <div> <p> <%= DateTime.Now.ToString() %></p ...

  9. json的工具以及浏览器排序问题

    浏览器中,所有涉及json的工具会按照键进行排序,这个与实际的查询的数组的顺序有出入,见下图:

  10. C++生成十字绣图案(二) 面向对象

    基本的十字绣线性生成中提供了判断下一步可以画的位置并且逐步生成的函数.以这些基本函数为基础,可以进行更多变化的图案设计. 为了方便的扩展,可以把线性生成写成一个类,以后的修改继承这个类. 头文件Bas ...