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.

问题:给定一个无序数组,找出数组排序后的相邻元素最大间隔。要求 O(n) 时间复杂度,O(n)空间复杂度

解题思路:

思路一:将所有数组全部排序,再一次遍历求得最大的相邻元素间隔值即可。但是排序算法的最小时间复杂度也需要 O(n*logn) ,无法满足要求。

思路二:题目只需要求出最大相邻间隔,可以利用桶排序,避免求出全部元素大小顺序,而得到结果。时间复杂度降低为 O(n)。

具体思路:

  • 将元素全部减去最小值,得到新的数组
  • 根据数组长度,创建同样大小的桶(数组表示),并根据数组的数值区间、数组长度得到单个桶的大小
  •   double bucketSize = (double)(maxv-minv) / (double)nums.size();
  • 根据元素大小,将元素放到对应的桶里面。数组中最大值,最小值应该分别在最左边、最右边的两个桶里面。
  •   int idx = newNums[i] / bucketSize;
  •   bucket[idx].push_back(newNums[i]);

// 断言 : 最大间隔值为桶间间隔的最大值。

// 断言证明:当每个桶都有值时,每个桶只有一个值,断言成立。当至少有一个桶为空时,因为最左边、最右边两个桶都有值,则最大间隔必然为桶间间隔,而不是桶内间隔,断言成立。

  • 去掉每个桶中最大值、最小值之外的其他值
  • 一次遍历求得最大桶间间隔,即为原题解。
     int maximumGap(vector<int>& nums) {

         if (nums.size() < ) {
return ;
} int maxv = nums[];
int minv = nums[];
for (int i = ; i < nums.size(); i++) {
maxv = max(maxv, nums[i]);
minv = min(minv, nums[i]);
} vector<int> newNums;
for (int i = ; i < nums.size(); i++) {
newNums.push_back(nums[i] - minv);
} vector<vector<int>> bucket(nums.size() + ); double bucketSize = (double)(maxv-minv) / (double)nums.size(); // maxv == minv
if (bucketSize == ) {
return ;
} for (int i = ; i < newNums.size(); i++) {
int idx = newNums[i] / bucketSize;
bucket[idx].push_back(newNums[i]);
} for (int i = ; i < bucket.size(); i++) {
if (bucket[i].size() >= ) {
int maxi = bucket[i][];
int mini = bucket[i][];
for (int k = ; k < bucket[i].size(); k++ ) {
maxi = max(maxi, bucket[i][k]);
mini = min(mini, bucket[i][k]);
}
bucket[i] = {mini, maxi};
}
} int maxgap = ; int lmaxi = (bucket[].size() == ) ? bucket[][] : bucket[][]; for (int i = ; i < bucket.size(); i++) {
if (bucket[i].size() == ) {
continue;
}
int maxi;
int mini; if (bucket[i].size() == ) {
maxi = bucket[i][];
mini = bucket[i][];
}else{
// size of bucket[i] shoud be 2; mini = bucket[i][];
maxi = bucket[i][];
} if ((mini - lmaxi) > maxgap) {
maxgap = (mini - lmaxi);
}
lmaxi = maxi;
} return maxgap;
}

[LeetCode] Maximum Gap 解题思路的更多相关文章

  1. [LeetCode] 53. Maximum Subarray 解题思路

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

  2. [LeetCode] Word Break 解题思路

    Given a string s and a dictionary of words dict, determine if s can be segmented into a space-separa ...

  3. [LeetCode] Maximum Gap 求最大间距

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

  4. [LeetCode] Distinct Subsequences 解题思路

    Given a string S and a string T, count the number of distinct subsequences of T in S. A subsequence ...

  5. [Leetcode] Maximum Gap

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

  6. [LeetCode] Decode Ways 解题思路

    A message containing letters from A-Z is being encoded to numbers using the following mapping: 'A' - ...

  7. LeetCode: Maximum Subarray 解题报告

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

  8. [LeetCode] Interleaving String 解题思路

    Given s1, s2, s3, find whether s3 is formed by the interleaving of s1 and s2. For example,Given:s1 = ...

  9. LeetCode Two Sum 解题思路(python)

    问题描述 给定一个整数数组, 返回两个数字的索引, 使两个数字相加为到特定值. 您可以假设每个输入都有一个解决方案, 并且您不能使用相同的元素两次. 方法 1: 蛮力 蛮力方法很简单.循环遍历每个元素 ...

随机推荐

  1. 怎样将关系型数据表转换至hbase数据表

    首先须要把关系型数据库的数据表的数据添加由 "纵向延伸",转变为HBase数据表的"横向延伸" 一.Hbase的存储结构 a)      HBase以表(HTa ...

  2. bzoj 3831 Little Bird (单调队列优化dp)

    /*先贴个n*n的*/ #include<iostream> #include<cstdio> #include<cstring> #define maxn 100 ...

  3. CSS3 target 伪类不得不说那些事儿(纯CSS实现tab切换)

    是不是觉得target有点眼熟?! 今天要讲的不是HTML的<a>标签里面有个target属性. target伪类是css3的新属性. 说到伪类,对css属性的人肯定都知道:hover.: ...

  4. 工作中部署使用MP平台的一些问题

    1.首先先把项目导入到myeclipse中,如果没有.classpath和.mymetadata和.project等文件,就自己创建一个web项目,然后把里面的src覆盖,webroot等文件覆盖. ...

  5. VM下Linux网卡丢失(pcnet32 device eth0 does not seem to be ...)解决方案

    系统启动日志:Bringing up interface eth0: pcnet32 device eth0 does not seepresent, delaying initialization. ...

  6. 数据库(批处理, 事务,CachedRawSetImpl类

    链接对象son产生的Statement SQL对象对数据库提交的任何一条语句都会被立刻执行 不方便我们进行一些连招操作 我们可以关闭它的自动提交,然后操作完再开,这过程称作事务 con.setAuto ...

  7. 信息增益(IG,Information Gain)的理解和计算

    决策树构建中节点的选择靠的就是信息增益了. 信息增益是一种有效的特征选择方法,理解起来很简单:增益嘛,肯定是有无这个特征对分类问题的影响的大小,这个特征存在的话,会对分类系统带来多少信息量,缺了他行不 ...

  8. C#操作Excel文件(转)

    摘要:本文介绍了Excel对象.C#中的受管代码和非受管代码,并介绍了COM组件在.net环境中的使用. 关键词:受管代码:非受管代码:Excel对象:动态连接库 引言 Excel是微软公司办公自动化 ...

  9. [Python笔记]第一篇:基础知识

    本篇主要内容有:什么是python.如何安装python.py解释器解释过程.字符集转换知识.传参.流程控制 初识Python 一.什么是Python Python是一种面向对象.解释型计算机程序设计 ...

  10. You don't have permission to access /phpmyadmin/main.php on this server.

    wamp  安装后,打开首页.出现问题,信息如下: “You don't have permission to access /phpmyadmin/main.php on this server.” ...