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 ...
随机推荐
- Office2019都有哪些强大功能
前阵子是微软一年一度的Ignite大会.而其中最引人注目.也是与我们一般人最息息相关的消息,当然是Office 2019的正式发布. 尽管Office 2019所更新的功能,对于Office 365的 ...
- opencv-3.3安装记录-ubuntu 14.04
这个二逼问题不会是最后一次. ipcv-****.tar.gz 这个文件在cmake的时候会卡住,这里先下载这个文件,大概38M,放到.cache/ippcv目录下就可以了.貌似还需要改下名字. 就可 ...
- MySQL更新优化(转)
通常情况下,当访问某张表的时候,读取者首先必须获取该表的锁,如果有写入操作到达,那么写入者一直等待读取者完成操作(查询开始之后就不能中断,因此允许读取者完成操作).当读取者完成对表的操作的时候,锁就会 ...
- 跨域(一)——CORS机制
Ajax是严格遵守同源策略的,既不能从另一个域读取数据,也不能发送数据到另一个域.但是,W3C的新标准中CORS(Cross Origin Resource Sharing)推进浏览器支持这样的跨域方 ...
- oracle vm突然黑屏了
装完mongodb-compass后,我重启了下虚拟机,发现突然进不到ubuntu系统了,一开起来就黑屏.网上查了下有的说是显卡问题的,有说是内核问题的,但我啥都没干突然间就黑屏了.折腾了一下午没搞定 ...
- Chrome 不能访问tensorboard解决
Chrome 不能访问tensorboard解决 Run: Cmd Result: C:\Users\think>tensorboard --logdir=C:\Users\think\sour ...
- tabel 选中行变色和取当前选中行值等问题
先把代码贴出来 $("#tableId tbody tr").mousedown(function () { $('#tableId tr').each(funct ...
- cv2对图像进行旋转和放缩变换
旋转: def get_image_rotation(image): #通用写法,即使传入的是三通道图片依然不会出错 height, width = image.shape[:2] center = ...
- HTML 视频
在html5中使用视频,只要添加元素<video></video>元素表示. <video>有几个属性: src 指定音频文件的路径 poster 视频播放之前显示 ...
- 用工厂模式和策略模式优化过多的if-else
多个if-else代码: @RunWith(SpringRunner.class) @SpringBootTest public class EducationalBackgroundTest { p ...