[LeetCode] Maximum Gap 解题思路
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 解题思路的更多相关文章
- [LeetCode] 53. Maximum Subarray 解题思路
Find the contiguous subarray within an array (containing at least one number) which has the largest ...
- [LeetCode] Word Break 解题思路
Given a string s and a dictionary of words dict, determine if s can be segmented into a space-separa ...
- [LeetCode] Maximum Gap 求最大间距
Given an unsorted array, find the maximum difference between the successive elements in its sorted f ...
- [LeetCode] Distinct Subsequences 解题思路
Given a string S and a string T, count the number of distinct subsequences of T in S. A subsequence ...
- [Leetcode] Maximum Gap
Given an unsorted array, find the maximum difference between the successive elements in its sorted f ...
- [LeetCode] Decode Ways 解题思路
A message containing letters from A-Z is being encoded to numbers using the following mapping: 'A' - ...
- LeetCode: Maximum Subarray 解题报告
Maximum Subarray Find the contiguous subarray within an array (containing at least one number) which ...
- [LeetCode] Interleaving String 解题思路
Given s1, s2, s3, find whether s3 is formed by the interleaving of s1 and s2. For example,Given:s1 = ...
- LeetCode Two Sum 解题思路(python)
问题描述 给定一个整数数组, 返回两个数字的索引, 使两个数字相加为到特定值. 您可以假设每个输入都有一个解决方案, 并且您不能使用相同的元素两次. 方法 1: 蛮力 蛮力方法很简单.循环遍历每个元素 ...
随机推荐
- PowerDesigner 生成带凝视SQL 各个版本号通用10(12、15)
做数据库是设计时最苦恼的事就是用PowerDesigner工具设计完数据库运行SQL文件后没有凝视.那么怎么才干让PowerDesigner设计完有凝视呢,下边教你一个笨的方法,方法尽管笨,可是能实现 ...
- [转] splice系列系统调用
关注splice系列系统调用(包括splice,tee和vmsplice)已经有一段时间了,开始的时候并未能领会splice的意义所在,致使得出了“splice系列系统调用不怎么实用”的错误结论.随着 ...
- angularjs手动解析表达式($parse)
<!DOCTYPE html> <html lang="zh-CN" ng-app="app"> <head> <me ...
- JavaScript 总结
1. JavaScript prototype属性是一个对象 当一个函数在定义之后 就会自动获得这个属性.其初始值是一个空对象.新建了一个名为Cat的构造函数,其prototype为一个对象,cons ...
- yii criteria select column as 与 时间段查询
需要查询某时间段的记录,但是数据库里只有一个时间记录,如果写sql的话,很快的,放到yii里一时竟然没办法... 不过,最后还是解决了,使用了一个第三方的插件 参考http://www.yiifram ...
- mybatis 自动生成xml文件配置
http://blog.csdn.net/techbirds_bao/article/details/9233599/
- 编程小计——消除Graphics图像边缘颜色不纯(抗锯齿)
在很多时候,我们都要绘制纯色的图片,而用Graphics生成的往往是不纯的,尤其是绘制文字时.比如说绘制纯红色文字,往往R达不到255. C#中默认抗锯齿,给人看起来柔和:但是我们现实中往往用到锯齿. ...
- adb服务无法启动
今天学习android编程发现调试出错 The connection to adb is down, and a severe error has occured. You must restart ...
- 关于ISAPI和CGI限制,这个要设为允许
否则程序就报这个错误,注意,设置允许时不是在添加的网站上设置,而是在根iis,选择后右侧出现关于ISAPI和CGI限制,进去后选择相应版本,设置为允许就可以了
- MyEclipse汉化后问题
今天为了教学生如何汉化MyEclipse10.7,所以讲IDE汉化了一下. 个人还是喜欢用英文版,所以就将D:\MyEclipse\MyEclipse 10目录下的配置文件myeclipse.ini里 ...