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. jdbc-手写Java方法连接数据库

    一.关键四元素   ①    com.mysql.jdbc.Driver      mysql数据库连接jar包.   获取途径: 链接:https://pan.baidu.com/s/1SFcjuu ...

  2. thinkphp5--关于多条件查询的分页处理问题

    首先,我们要想搞明白,我们的分页参数起作用的原理: 正在使用的时候的语法: if(!empty($seach)){ $where['user_name|mobile'] = ['like','%'.$ ...

  3. CG-CTF(6)

    CG-CTF https://cgctf.nuptsast.com/challenges#Web 续上~ 第三十一题:综合题2 查看本CMS说明: 分析: ①数据库表名为admin:字段名为usern ...

  4. dns的抓包分析

    dns: 域名系统(服务)协议 dns的解析全过程: 1. 浏览器先检查自身缓存中有没有被解析过的这个域名对应的ip地址,如果有,解析结束.同时域名被缓存的时间也可通过TTL属性来设置. 2. 如果浏 ...

  5. 安装 wbemcli

    安装环境        建立自己的目录后,             wget http://vault.centos.org/6.0/os/x86_64/Packages/sblim-wbemcli- ...

  6. Windows SDK 的 ctype.h 里有一些有用的函数,例如字符串的大小写转换

    #define _tolower(c) ((c) - 'A' + 'a') #define _toupper(c) ((c) - 'a' + 'A')

  7. 在java中使用JMH(Java Microbenchmark Harness)做性能测试

    文章目录 使用JMH做性能测试 BenchmarkMode Fork和Warmup State和Scope 在java中使用JMH(Java Microbenchmark Harness)做性能测试 ...

  8. C++操作Kafka使用Protobuf进行跨语言数据交互

    C++操作Kafka使用Protobuf进行跨语言数据交互 Kafka 是一种分布式的,基于发布 / 订阅的消息系统.主要设计目标如下: 以时间复杂度为 O(1) 的方式提供消息持久化能力,即使对 T ...

  9. 「每天一道面试题」Java类的生命周期包括哪几个阶段?

    一个Java类被加载到虚拟机中,它的生命周期才算开始,直到被从内存中卸载,它的生命周期才算结束.从开始到结束,它的整个生命周期包括加载.验证.准备.解析.初始化.使用和卸载7个阶段,其中验证.准备和解 ...

  10. 51 NOD 1049 最大子段和 动态规划 模板 板子 DP

    N个整数组成的序列a[1],a[2],a[3],-,a[n],求该序列如a[i]+a[i+1]+-+a[j]的连续子段和的最大值.当所给的整数均为负数时和为0. 例如:-2,11,-4,13,-5,- ...