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)的更多相关文章

  1. Maximum Gap (ARRAY - SORT)

    QUESTION Given an unsorted array, find the maximum difference between the successive elements in its ...

  2. LeetCode 164. Maximum Gap[翻译]

    164. Maximum Gap 164. 最大间隔 Given an unsorted array, find the maximum difference between the successi ...

  3. leetcode[164] Maximum Gap

    梅西刚梅开二度,我也记一题. 在一个没排序的数组里,找出排序后的相邻数字的最大差值. 要求用线性时间和空间. 如果用nlgn的话,直接排序然后判断就可以了.so easy class Solution ...

  4. 【LeetCode】164. Maximum Gap (2 solutions)

    Maximum Gap Given an unsorted array, find the maximum difference between the successive elements in ...

  5. 【刷题-LeetCode】164 Maximum Gap

    Maximum Gap Given an unsorted array, find the maximum difference between the successive elements in ...

  6. [LeetCode] 164. Maximum Gap 求最大间距

    Given an unsorted array, find the maximum difference between the successive elements in its sorted f ...

  7. ✡ leetcode 164. Maximum Gap 寻找最大相邻数字差 --------- java

    Given an unsorted array, find the maximum difference between the successive elements in its sorted f ...

  8. 164. Maximum Gap

    题目: Given an unsorted array, find the maximum difference between the successive elements in its sort ...

  9. Java for LeetCode 164 Maximum Gap

    Given an unsorted array, find the maximum difference between the successive elements in its sorted f ...

随机推荐

  1. POI解析大量数据

    参考:https://blog.csdn.net/whandgdh/article/details/80267674

  2. Linux的学习 --corntab

    计划任务的使用方法 http://www.cnblogs.com/CraryPrimitiveMan/p/4124851.html

  3. git的团队协作开发

    title: git的团队协作开发 date: 2018-04-24 14:00:03 tags: [git] --- 项目负责人创建组织架构 在控制面板中点击组织按钮,添加组织,在这里可以把组织理解 ...

  4. ADOQuery.Parameters: Property Parameters does not exist

    Exception class EReadError with message 'Property Parameters does not exist'. Exception class EReadE ...

  5. delphi面向对象 继承窗体

    delphi继承form TFrmBase = class(TForm) procedure FormShow(Sender: TObject); end; procedure TFrmBase.Fo ...

  6. sql 2014 安装失败

    SQL Server setup failed to modify security permissions on 原因是 上述目录中没有权限,浏览此文件夹试试,有 错误,删除文件夹,无权删除,通过右 ...

  7. delphi query阻塞执行 长时间执行sql的解决办法

    delphi query 执行sql一直是阻塞执行,执行长时间的sql语句,程序没响应了,这时候只能用线程技术解决. 如今FDQuery有了CmdExecMode属性,可以设置amCancelDial ...

  8. 机器学习进阶-人脸关键点检测 1.dlib.get_frontal_face_detector(构建人脸框位置检测器) 2.dlib.shape_predictor(绘制人脸关键点检测器) 3.cv2.convexHull(获得凸包位置信息)

    1.dlib.get_frontal_face_detector()  # 获得人脸框位置的检测器, detector(gray, 1) gray表示灰度图, 2.dlib.shape_predict ...

  9. 如何玩转小程序+公众号?手把手教你JeeWx小程序CMS与公众号关联

    随着微信小程序新功能.新入口的不断更新,小程序的商业价值逐步增强,特别是小程序与公众号的深度融合,已经让小程序成为各行业新的营销渠道.Jeewx平台专注小程序的开发,逐步完善小程序生态圈,通过简单操作 ...

  10. MySQL性能分析(转)

    第一步:检查系统的状态 通过操作系统的一些工具检查系统的状态,比如CPU.内存.交换.磁盘的利用率.IO.网络,根据经验或与系统正常时的状态相比对,有时系统表面上看起来看空闲,这也可能不是一个正常的状 ...