[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: 蛮力 蛮力方法很简单.循环遍历每个元素 ...
随机推荐
- 天圆地方· 围棋界的盲棋天才 -- 鲍云
"鲍云是我心目中继 本因坊秀策,吴清源.武宫正树后第四个我最喜欢的棋手. " 说到盲棋,棋迷们首先想到的绝对是柳大华,外号"东方电脑"的他创造过中国象棋1对19 ...
- 【网络流#7】POJ 3281 Dining 最大流 - 《挑战程序设计竞赛》例题
不使用二分图匹配,使用最大流即可,设源点S与汇点T,S->食物->牛->牛->饮料->T,每条边流量为1,因为流过牛的最大流量是1,所以将牛拆成两个点. 前向星,Dini ...
- Android Studio中常用设置与快捷键
常用设置: 1.Tab不用4个空格Code Style->Java->Tabs and Indents->Use tab characterCode Style->Genera ...
- linux性能分析命令top
发布时间: 2013-12-14浏览次数:154分类: 服务器 top是linux最常用的性能分析工具了,它是个交互式工具,提供系统的整体性能,如正在执行的进程信息包括进程ID,内存占用率,CPU占用 ...
- VS2010发布网站的基本步骤
1.首先建一个空文件夹,用来存放发布的程序:例如:WebTest 2.然后打开IIS--->右击--->添加网站,如下图所示: 图 1-1 ...
- iptables阻止服务器被攻击
下列规则将会阻止来自某一特定IP范围内的数据包,因为该IP地址范围被管理员怀疑有大量恶意攻击者在活动: # iptables -t filter -A INPUT -s 123.456.789.0/ ...
- mysql文件导入到数据库load data infile into table 的使用例子
load data infile "C:/Users/Administrator/Desktop/1.txt"into table 要一个已经存的表名 字段默认用制表符隔开 文件 ...
- 带左右箭头切换的自动滚动图片JS特效
效果图 按钮 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www. ...
- 使用shiro标签遇到的部分问题的解决思路
最近几天,在shiro进行系统权限控制.在处理JSP页面的时候,遇到几个问题,纠结好几天,终于成功解决这些问题. 1.使用<shiro:principal>的时候,如何得到整个类的信息? ...
- uva10020 贪心
题目链接:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem ...