[LeetCode] 164. 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.
遇到这类问题肯定先想到的是要给数组排序,但是题目要求是要线性的时间和空间,那么只能用桶排序或者基排序。这里用桶排序 Bucket Sort 来做,首先找出数组的最大值和最小值,然后要确定每个桶的容量,即为 (最大值 - 最小值) / 个数 + 1,在确定桶的个数,即为 (最大值 - 最小值) / 桶的容量 + 1,然后需要在每个桶中找出局部最大值和最小值,而最大间距的两个数不会在同一个桶中,而是一个桶的最小值和另一个桶的最大值之间的间距,这是因为所有的数字要尽量平均分配到每个桶中,而不是都拥挤在一个桶中,这样保证了最大值和最小值一定不会在同一个桶中,具体的证明博主也不会,只是觉得这样想挺有道理的,各位看官大神们若知道如何证明请务必留言告诉博主啊,参见代码如下:
class Solution {
public:
int maximumGap(vector<int>& nums) {
if (nums.size() <= ) return ;
int mx = INT_MIN, mn = INT_MAX, n = nums.size(), pre = , res = ;
for (int num : nums) {
mx = max(mx, num);
mn = min(mn, num);
}
int size = (mx - mn) / n + , cnt = (mx - mn) / size + ;
vector<int> bucket_min(cnt, INT_MAX), bucket_max(cnt, INT_MIN);
for (int num : nums) {
int idx = (num - mn) / size;
bucket_min[idx] = min(bucket_min[idx], num);
bucket_max[idx] = max(bucket_max[idx], num);
}
for (int i = ; i < cnt; ++i) {
if (bucket_min[i] == INT_MAX || bucket_max[i] == INT_MIN) continue;
res = max(res, bucket_min[i] - bucket_max[pre]);
pre = i;
}
return res;
}
};
Github 同步地址:
https://github.com/grandyang/leetcode/issues/164
参考资料:
https://leetcode.com/problems/maximum-gap
http://blog.csdn.net/u011345136/article/details/41963051
https://leetcode.com/problems/maximum-gap/discuss/50642/radix-sort-solution-in-java-with-explanation
LeetCode All in One 题目讲解汇总(持续更新中...)
[LeetCode] 164. Maximum Gap 求最大间距的更多相关文章
- LeetCode 164. Maximum Gap[翻译]
164. Maximum Gap 164. 最大间隔 Given an unsorted array, find the maximum difference between the successi ...
- [LintCode] Maximum Gap 求最大间距
Given an unsorted array, find the maximum difference between the successive elements in its sorted f ...
- leetcode[164] Maximum Gap
梅西刚梅开二度,我也记一题. 在一个没排序的数组里,找出排序后的相邻数字的最大差值. 要求用线性时间和空间. 如果用nlgn的话,直接排序然后判断就可以了.so easy class Solution ...
- [LeetCode] 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】164 Maximum Gap
Maximum Gap Given an unsorted array, find the maximum difference between the successive elements in ...
- 【leetcode】Maximum Gap
Maximum Gap Given an unsorted array, find the maximum difference between the successive elements in ...
随机推荐
- HDU 6148 (数位DP)
### HDU 6148 题目链接 ### 题目大意: 众所周知,度度熊非常喜欢数字. 它最近发明了一种新的数字:Valley Number,像山谷一样的数字. 当一个数字,从左到右依次看过去数字没有 ...
- javascript json写法
javascript json写法 var shuxing = {name:"super",sex:"19",work:"IT"}; 这个k ...
- Vue.js 源码分析(二十) 指令篇 v-once指令详解
数据绑定最常见的形式就是使用“Mustache”语法 (双大括号) 的文本插值,例如:<p>Message: {{ msg }}</p>以后每当msg属性发生了改变,插值处的内 ...
- ThinkPHP框架获取上一条插入语句产生的id
今天在fastAdmin框架想搞一个拖动进行排序的功能 遇到一个问题是权重的字段值一样的话拖动会出bug,所以想让权重字段(weigh)的值等于当前id的值, 搜索看到的方法如下 实际应用的地方,是写 ...
- Window权限维持(八):时间服务器
Windows操作系统正在利用时间提供者体系结构,以便从网络中的其他网络设备或客户端获取准确的时间戳.时间提供者以DLL文件的形式实现,该文件位于System32文件夹中.Windows启动期间将启动 ...
- .net core 3.0中的Json API
在.net core 3.0中,内置了一套新的json api,主要用于去除asp.net core对json.net的依赖,同时也提供了更好的性能(直接处理 UTF-8,而无需转码到 UTF-16) ...
- C# NPOI Excel
using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threa ...
- Grafana的Docker部署方式
docker run -d -p : --name=grafana544 -v D:/grafana/grafana-/data:/var/lib/grafana -v D:/grafana/graf ...
- webapi 导入excel处理数据
参考资料 https://blog.csdn.net/pan_junbiao/article/details/82935992 https://www.cnblogs.com/dansedia ...
- JPA技术之EntityManager使用方法
Session bean or MD bean对Entity bean的操作(包括所有的query, insert, update, delete操作)都是通过EntityManager实例来完成的. ...