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. git 找回本地误删的文件

    1, 查看本地工作区变化 => git status 2, 重新设置文件状态 =>  git reset HEAD url    ( url 是上一步第二个红框中的地址) 3, 检出文件 ...

  2. delphi多语言

    LoadLangFromStrings http://docwiki.embarcadero.com/Libraries/Berlin/en/FMX.Types.TLang http://blog.c ...

  3. E2040 Declaration terminated incorrectly - System.ZLib.hpp(310) ZLIB_VERSION

    [bcc32 Error] System.ZLib.hpp(310): E2040 Declaration terminated incorrectly  Full parser context    ...

  4. 使用Prometheus+Grafana监控MySQL实践

    一.介绍Prometheus Prometheus(普罗米修斯)是一套开源的监控&报警&时间序列数据库的组合,起始是由SoundCloud公司开发的.随着发展,越来越多公司和组织接受采 ...

  5. WeakReference与SoftReference

    WeakReference与SoftReference都可以用来保存对象的实例引用,这两个类与垃圾回收有关. WeakReference是弱引用,其中保存的对象实例可以被GC回收掉.这个类通常用于在某 ...

  6. Service的启动,绑定,绑定后通信,跨应用绑定,跨应用绑定后通信

    Service介绍: 与Acitivity平级的Android四大组件之一,对比Activity而言,就是没有交互界面,且可以一直在后台执行,只用于处理下载,I/O等长时间信息交流的任务. Servi ...

  7. ReactiveX 学习笔记(14)使用 RxJava2 + Retrofit2 调用 REST API

    JSON : Placeholder JSON : Placeholder (https://jsonplaceholder.typicode.com/) 是一个用于测试的 REST API 网站. ...

  8. Python读取图片尺寸、图片格式

    Python读取图片尺寸.图片格式 需要用到PIL模块,使用pip安装Pillow.Pillow是从PIL fork过来的Python 图片库. from PIL import Image im = ...

  9. C++学习二继承

    转载自https://www.cnblogs.com/33debug/p/6666939.html 1.继承与派生  继承是使代码可以复用的重要手段,也是面向对象程序设计的核心思想之一.简单的说,继承 ...

  10. Javascript中构造函数的返回值问题和new对象的过程

    首先明确一点:javascript中构造函数是不需要有返回值的,这一点跟java很类似.可以认为构造函数和普通函数的最大差别就是:构造函数中没有return语句,普通函数可以有return语句:构造函 ...