[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: 蛮力 蛮力方法很简单.循环遍历每个元素 ...
随机推荐
- iOS 在 Xcode 中重命名项目名称
本教程使用的 Xcode 版本是Xcode 6.3.1,网上有好多的教程,都是在 Xcode 4 上做的讲解,现以本文章讲解一下如何在 Xcode 6.3.1 中重命名你的项目名称,包括你的应用名称. ...
- 在Windows环境中安装并使用kafka
[TOC] 安装部署 安装部署Java 下载需要安装的软件,下载地址:http://www.oracle.com/technetwork/java/javase/downloads/jre8-down ...
- Android中使用ListView绘制自定义表格(2)
上回再写了<Android中使用ListView绘制自定义表格>后,很多人留言代码不全和没有数据样例.但因为项目原因,没法把源码全部贴上来.近两天,抽空简化了一下,做了一个例子. 效果图如 ...
- Android Studio 安装
准备: JDK 7以及以上版本. Android Studio安装文件 中文站下载 http://www.android-studio.org/index.php/download exe ,包含S ...
- 使用HighCharts实现实时数据展示
在众多的工业控制系统领域常常会实时采集现场的温度.压力.扭矩等数据,这些数据对于监控人员进行现场态势感知.进行未来趋势预测具有重大指导价值.工程控制人员如果只是阅读海量的数据报表,对于现场整个态势的掌 ...
- 图片跟着鼠标动js
<!DOCTYPE html><html><head> <title>duisgf</title> <meta charset=&qu ...
- Arcgis server - ' packaging failed '
我在使用ARCCatalog发布地图服务时,报这个错:packaging failed 然后我从头试,发现它提示说我的目录'C:\Users\Administrator\AppData\Local\E ...
- linux FILE 类型.
stdio.h 头文件中,有以下内容(用内部行号解释): /* The opaque type of streams. This is the definition used elsewhere. * ...
- Cacti监控Linux主机
1 要监视一台Linux主机,需要在被监控的主机上安装net-snmp相关软件包,CentOS安装可使用“yum -y install net-snmp”命令:# yum -y install net ...
- ORA-16014: 日志 1 的序列号 242 未归档, 没有可用的目的地
SQL> alter database open; *第 1 行出现错误:ORA-16014: 日志 1 的序列号 242 未归档, 没有可用的目的地ORA-00312: 联机日志 1 线程 1 ...