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

Return 0 if the array contains less than 2 elements.

  • Try to solve it in linear time/space.

题目来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/maximum-gap
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

=======================================================================

【分析】

这道题目最简单的思路就是使用自带的排序函数先排序,然后比较计算相邻量元素差值并求出最大值。时间复杂度为O(nlogn)。但题目中提到最好用线性时间复杂度来解决,引入一种更快捷的排序方法,基数排序。

基数排序:

1. 从最低位(或最高位)开始,根据每个元素该为数字大小进行排序(若该为相等的元素则维持原有的前后顺序);

2.组成新的数组后再根据高一位的数字大小对元素按相同方法进行排序;

3.直到根据数组中最大的数字的最高位排序后,即可得到一个有序数组。

Eg. 原数组:

[ 10, 22, 19, 43, 72, 1, 8, 312]

根据个位数字排序后变为: [10, 1, 22, 72, 312, 43, 8, 19]

根据十位数字排序后变为: [1, 8, 10, 312, 19, 22, 43, 72]

根据百位数字排序后变为: [1, 8, 10, 19, 22, 43, 72, 312]

【参考代码】

class Solution {
public:
int maximumGap(vector<int>& nums) {
int n = nums.size();
if(n < ) return ;
vector<int> aux(n, );
int exp = ; int maxN = *max_element(nums.begin(), nums.end());
while(maxN/exp > ) {
vector<int> count(, );
for(int n: nums) {
count[(n/exp)%]++;
}
for(int i = ; i < ; i++) {
count[i] += count[i-];
}
for(int i = n-; i >= ; --i) {
int n = nums[i];
aux[--count[(n/exp)%]] = n;
}
for(int i = ; i < n; ++i) {
nums[i] = aux[i];
}
exp *= ;
}
int res = ; for(int i = ; i < n; ++i) {
//cout << nums[i-1] << ", ";
res = max(res, nums[i]-nums[i-]);
}
return res;
}
};

【代码分析】

使用count[d]. 先用count[d]来统计当前的位数中数字为d的元素个数,后进行处理,使count[d]表示小于等于d的元素总数。

之后可从后向前遍历nums, 根据元素当前位数中的数字决定他应该在新数组(aux)中的位置:

最后一个当前位等于d的元素,应该处于count[d]-1的位置;放入该元素后,把count[d]减一,便为下一个当前位等于d的元素的位置+1。

时间复杂度:O(d*(n+10)) 约等于O(n)

【Leetcode】164. Maximum Gap 【基数排序】的更多相关文章

  1. LeetCode 164. Maximum Gap[翻译]

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

  2. leetcode[164] Maximum Gap

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

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

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

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

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

  5. Java for LeetCode 164 Maximum Gap

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

  6. 【刷题-LeetCode】164 Maximum Gap

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

  7. 【LeetCode】164. Maximum Gap (2 solutions)

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

  8. 【leetcode】Maximum Gap

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

  9. 【leetcode】Maximum Gap(hard)★

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

随机推荐

  1. UDO、TCP、HTTP、websocket

    如图:

  2. The new SFCB broker fails to start with a SSL-related error: Failure setting ECDH curve name (secp22

    # openssl ecparam -list_curves secp384r1 : NIST/SECG curve over a 384 bit prime field secp521r1 : NI ...

  3. 【Linux常见命令】uname命令

    uname命令用于显示系统信息. uname可显示电脑以及操作系统的相关信息. 语法 uname [-amnrsv][--help][--version] 参数说明: -a或--all 显示全部的信息 ...

  4. 20060518: Alert!

    Alert Received, Shrink My Blog! 转载于:https://www.cnblogs.com/yidinghe/archive/2006/05/18/403089.html

  5. TCP连接过程及报文解析

    可能大家都听过TCP建立连接时需要经历三次握手和四次挥手的. 那么具体的握手挥手的过程是怎么样的呢? 这篇文章就通过WireShark抓包来了解TCP连接建立和断开的过程. 实验方法: 写一段简单的代 ...

  6. 2019-2020 ICPC, Asia Jakarta Regional Contest A. Copying Homework (思维)

    Danang and Darto are classmates. They are given homework to create a permutation of N integers from  ...

  7. muduo网络库源码学习————线程特定数据

    muduo库线程特定数据源码文件为ThreadLocal.h //线程本地存储 // Use of this source code is governed by a BSD-style licens ...

  8. 使用npm发布插件

    使用npm发布插件 博客说明 文章所涉及的资料来自互联网整理和个人总结,意在于个人学习和经验汇总,如有什么地方侵权,请联系本人删除,谢谢! 简介 npm是一个全球性的包管理工具,上面有着许许多多的前端 ...

  9. 麦基数(p1045)

    描述: \(计算2^{P}−1的位数和最后500位数字(用十进制高精度数表示)\) Ⅰ.求位数 \(因为2^p最后一位必定不为0,求2^p-1的位数也就是求2^p位数\) \(2^p的位数确实很难求, ...

  10. 补 第三场多校杭电 费用流 K Subsequence

    K Subsequence 这个题目是这个人想吃东西,但是他每次吃的都是他的美味值都必须不递减,可以吃k次,问这个最大的美味值是多少. 这个是一个比较明显的费用流,建图也很好建,但是呢,这个题目卡sp ...