164. Maximum Gap (Array; sort)
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)时间复杂度,所以不能用快排等排序方法,使用桶排序(bucket sort)
class Solution {
public:
int maximumGap(vector<int>& nums) {
int size = nums.size();
if(size < ) return ;
if(size == ) return abs(nums[]-nums[]);
int maxValue=INT_MIN, minValue = INT_MAX;
for(int i = ; i < size; i++){
if(nums[i]>maxValue) maxValue = nums[i];
if(nums[i]<minValue) minValue = nums[i];
}
//determine the number of buckets (on average, one element in on bucket)
int avgGap = ceil((double)(maxValue - minValue) / (size-)); // 平均间隔
if(avgGap == ) return ;
int bucketNum = ceil((double)(maxValue - minValue) / avgGap);
int bucketIndex;
vector<pair<int, int>> buckets(bucketNum, make_pair(INT_MIN, INT_MAX)); // 初始化桶, save max and min of each bucket
for(int i = ; i < size; i++){
//the last element(maxValue) should be dealt specially, for example [100,1,2,3],otherwise its index will be out of bound.
if(nums[i] == maxValue) continue;
//determine the bucket index
bucketIndex = (nums[i]-minValue) / avgGap;
if(nums[i]>buckets[bucketIndex].first) buckets[bucketIndex].first = nums[i]; //update max of the bucket
if(nums[i]<buckets[bucketIndex].second) buckets[bucketIndex].second = nums[i]; //update min of the bucket
}
//max difference must exists between buckets if there're more than one bucket(because in buckets, gap at maximum = avgGap)
int preIndex = ;
int maxGap = buckets[preIndex].first - minValue;;
int gap;
for(int i = preIndex+; i < bucketNum; i++){
if(buckets[i].first == INT_MIN) continue; //ignore empty
gap = buckets[i].second-buckets[preIndex].first;
if(gap > maxGap) {
maxGap = gap;
}
preIndex = i;
}
gap = maxValue - buckets[preIndex].first;
if(gap > maxGap) {
maxGap = gap;
}
return maxGap;
}
};
164. Maximum Gap (Array; sort)的更多相关文章
- Maximum Gap (ARRAY - SORT)
QUESTION Given an unsorted array, find the maximum difference between the successive elements in its ...
- 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 (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] 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 ...
- 164. Maximum Gap
题目: Given an unsorted array, find the maximum difference between the successive elements in its sort ...
- Java for LeetCode 164 Maximum Gap
Given an unsorted array, find the maximum difference between the successive elements in its sorted f ...
随机推荐
- HTML5 图片宽高自适应,居中裁剪不失真
一,使用 JS,先上效果图,右图为定死宽高的效果,左图为处理之后的 1, 主要思路是,在图片 onload 后,将图片的宽高比和 div 容器的宽高比进行比较, 2, 从而确定拉伸或者压缩之后是宽还是 ...
- <记录> PHP Redis操作类
namespace common\controller; class Redis { public $redisObj = null; //redis实例化时静态变量 static protected ...
- Oracle的基本数据类型(常用)
转自:https://www.2cto.com/database/201810/783959.html 1.字符型 Char 固定长度字符串 占2000个字节 Varchar2 可变长度字符串 占40 ...
- 机器学习进阶-图像形态学操作-梯度运算 cv2.GRADIENT(梯度运算-膨胀图像-腐蚀后的图像)
1.op = cv2.GRADIENT 用于梯度运算-膨胀图像-腐蚀后的图像 梯度运算:表示的是将膨胀以后的图像 - 腐蚀后的图像,获得了最终的边缘轮廓 代码: 第一步:读取pie图片 第二步:进行腐 ...
- Bootstarp 模版网站
最佳Bootstrap模版 https://colorlib.com/wp/cat/bootstrap/ https://www.jianshu.com/p/4a116cf24a05
- 04_JSX练习
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- js 引入外部文件之 script 标签
在我的理解看来,html 就是一个单纯的管显示问题,js就是单纯的管动作问题,css就是单纯的管布局问题,这三个构成了一个网页 在HTML中,经常会用到引入js 文件. 引入js的方法很简单: 1. ...
- 服务发现 - consul 的介绍、部署和使用(转)
什么是服务发现 相关源码: spring cloud demo 微服务的框架体系中,服务发现是不能不提的一个模块.我相信了解或者熟悉微服务的童鞋应该都知道它的重要性.这里我只是简单的提一下,毕竟这不是 ...
- Spring/SpringBoot定义统一异常错误码返回
配置 大致说下流程, 首先我们自定义一个自己的异常类CustomException,继承RuntimeException.再写一个异常管理类ExceptionManager,用来抛出自定义的异常. 然 ...
- 自定义标签在IE6-8的困境
或许未来前端组件化之路都是自定义标签,但这东西早在20年前,JSTL已在搞了.现在Web Component还只有webkit支持.但一个组件库,还需要一个特殊的标识它们是一块的.不过这个XML已经帮 ...