【刷题-LeetCode】164 Maximum Gap
- Maximum Gap
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.
Example 1:
Input: [3,6,9,1]
Output: 3
Explanation: The sorted form of the array is [1,3,6,9], either
(3,6) or (6,9) has the maximum difference 3.
Example 2:
Input: [10]
Output: 0
Explanation: The array contains less than 2 elements, therefore return 0.
Note:
- You may assume all elements in the array are non-negative integers and fit in the 32-bit signed integer range.
- Try to solve it in linear time/space.
解法1 \(O(n\log n)\)排序
class Solution {
public:
int maximumGap(vector<int>& nums) {
sort(nums.begin(), nums.end());
int res = 0;
for(int i = 1; i < nums.size(); ++i)res = max(res, nums[i]-nums[i-1]);
return res;
}
};
解法2 \(O(n)\)排序算法:桶排序、计数、基数。max-min可能会比较大,计数排序可能需要很大的空间,实现后发现会超时。
解法2.1 基数排序
基数排序是按照低位先排序,然后收集;再按照高位排序,然后再收集;依次类推,直到最高位。有时候有些属性是有优先级顺序的,先按低优先级排序,再按高优先级排序。最后的次序就是高优先级高的在前,高优先级相同的低优先级高的在前。

class Solution {
public:
int maximumGap(vector<int>& nums) {
if(nums.size() < 2)return 0;
radix_sort(nums);
int res = 0;
for(int i = 1; i < nums.size(); ++i)res = max(res, nums[i]-nums[i-1]);
return res;
}
void radix_sort(vector<int>& nums){
int maxVal = *max_element(nums.begin(), nums.end());
int exp = 1, radix = 10;
vector<int>aux(nums.size());
while(maxVal / exp > 0){
vector<int>cnt(radix, 0);
for(int i = 0; i < nums.size(); ++i){
int idx = (nums[i] / exp) % radix;
cnt[idx]++;
}
for(int i = 1; i < cnt.size(); ++i)cnt[i] += cnt[i-1];
for(int i = nums.size() - 1; i >= 0; --i){
aux[--cnt[(nums[i] / exp) % 10]] = nums[i];
}
for(int i = 0; i < nums.size(); ++i)nums[i] = aux[i];
exp *= 10;
}
}
};
解法2.2 桶排序
间隔为d,则\(\mathrm{d} \geq \frac{n\_max - n\_min}{n-1} = b\)。因此设置n-1个桶,桶高为\(b\),每个桶存储最小和最大的元素,最后依次比较使用过的相邻两个桶的间隔,取最大值
class Solution {
public:
int maximumGap(vector<int>& nums) {
if(nums.size() < 2)return 0;
int n_max = *max_element(nums.begin(), nums.end());
int n_min = *min_element(nums.begin(), nums.end());
int b = max(1, int((n_max - n_min) / (nums.size() - 1)));
vector<vector<int>>cnt(int((n_max - n_min) / b) + 1, vector<int>(2));
for(auto &v : cnt){
v[0] = INT_MAX;
v[1] = INT_MIN;
}
for(int x : nums){
int idx = floor((x - n_min) / b);
cnt[idx][0] = min(cnt[idx][0], x);
cnt[idx][1] = max(cnt[idx][1], x);
}
int res = 0;
int pre_max = cnt[0][1];
for(int i = 1; i < cnt.size(); ++i){
if(cnt[i][0] == INT_MAX)continue;
res = max(res, cnt[i][0] - pre_max);
pre_max = cnt[i][1];
}
return res;
}
};
【刷题-LeetCode】164 Maximum Gap的更多相关文章
- LeetCode 164. Maximum Gap[翻译]
164. Maximum Gap 164. 最大间隔 Given an unsorted array, find the maximum difference between the successi ...
- leetcode[164] Maximum Gap
梅西刚梅开二度,我也记一题. 在一个没排序的数组里,找出排序后的相邻数字的最大差值. 要求用线性时间和空间. 如果用nlgn的话,直接排序然后判断就可以了.so easy class Solution ...
- [LeetCode] 164. Maximum Gap 求最大间距
Given an unsorted array, find the maximum difference between the successive elements in its sorted f ...
- ✡ leetcode 164. Maximum Gap 寻找最大相邻数字差 --------- java
Given an unsorted array, find the maximum difference between the successive elements in its sorted f ...
- Java for LeetCode 164 Maximum Gap
Given an unsorted array, find the maximum difference between the successive elements in its sorted f ...
- 【LeetCode】164. Maximum Gap (2 solutions)
Maximum Gap Given an unsorted array, find the maximum difference between the successive elements in ...
- LeetCode刷题------------------------------LeetCode使用介绍
临近毕业了,对技术有种热爱的我也快步入码农行业了,以前虽然在学校的ACM学习过一些算法,什么大数的阶乘,dp,背包等,但是现在早就忘在脑袋后了,哈哈,原谅我是一枚菜鸡,为了锻炼编程能力还是去刷刷Lee ...
- 【leetcode】Maximum Gap
Maximum Gap Given an unsorted array, find the maximum difference between the successive elements in ...
- 【Leetcode】164. Maximum Gap 【基数排序】
Given an unsorted array, find the maximum difference between the successive elements in its sorted f ...
随机推荐
- SP1798 ASSIST - Assistance Required 题解
Content 有一个足够长的数列 \(a\),是一个首项为 \(2\),公差为 \(1\) 的等差递增数列.另有一个初始为空的数列 \(b\). 重复进行如下操作: 假设当前数列 \(a\) 第一项 ...
- CF169A Chores 题解
Content 两兄弟要分担 \(n\) 件家务,第 \(i\) 件家务有一个复杂度 \(h_i\).兄弟俩以一个数 \(x\) 为界.所有满足复杂度 \(>x\) 的家务都给哥哥做,其余的给弟 ...
- Mac 远程上传文件到 Linux
打开Mac自带终端: 在最顶端选择Shell ->新建远程连接: 选择sftp连接,填好服务器地址: 连接成功后.上传文件,使用 put 命令: put 本地文件路径 远程主机路径
- shell判断新字符串列表是否在老字符串列表中
for sn in `cat 12.30-new`;do if ! [[ `cat 12.30-old` =~ $sn ]];then echo $sn; fi; done
- Linux(centos)安装es(elasticsearch)
前提条件--需要安装jdk环境,不同版本的es所对应的jdk版本要求不同,es6的使用jdk1.8可以 1.下载elasticsearch压缩包 下载地址:https://www.elastic.co ...
- 【LeetCode】797. All Paths From Source to Target 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 回溯法 日期 题目地址:https://leetco ...
- Docker 与 K8S学习笔记(六)—— 容器的资源限制
我们在启动Docker容器时,默认情况下容器所使用的资源是没有限制的,这样就会存在部分特别耗资源的容器会占用大量系统资源,从而导致其他容器甚至整个服务器性能降低,为此,Docker提供了一系列参数方便 ...
- P4081 [USACO17DEC]Standing Out from the Herd P
知识点: 广义 SAM 原题面 Luogu 「扯」 随便「口胡」一下居然「过」了. 比较考验「代码能力」,第一次感觉「大模拟」没有白写((( 还有这个「符号」实在是太「上头」了. 前置知识 在线构造广 ...
- Java EE数据持久化框架 • 【第5章 MyBatis代码生成器和缓存配置】
全部章节 >>>> 本章目录 5.1 配置MyBatis Generator 5.1.1 MyBatis Generator介绍 5.1.2 MyBatis Generat ...
- Vue-cli3.0配置全局less
第一种配置方式(推荐) npm install style-resources-loader vue-cli-plugin-style-resources-loader less-loader les ...