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. C# 类库调试 启动外部程序无法调试

    无法调试进程 test.exe [17936] 中的某些代码.请参阅下面的状态信息. IntelliTrace 代码失败(0x80131534).    Managed (v4.6.v4.5.v4.0 ...

  2. png 2 icon

    http://www.easyicon.net/covert/ 这个网页可以转换png图片为icon格式

  3. linux crontab 保证php守护进程运行

    写好php脚本.建议定期检测内存占用,核心逻辑就不写了.这个跟业务有关. if(memory_get_usage()>100*1024*1024){ exit(0);//大于100M内存退出程序 ...

  4. PHP中汉字截取

    $len = 19; $text = "怎么将新闻的很长的标题只显示前面一些字,后面用.....来代替?"; echo strlen($text)<=$len ? $text ...

  5. day08-字符串操作

    name = 'hello,world,WORLD! 123,你好' #capitalize()#首字母大写,其他全部变成小写,没有iscapitalize()方法print(name.capital ...

  6. mysqldump备份时保持数据一致性

    对MySQL数据进行备份,常见的方式如以下三种,可能有很多人对备份时数据一致性并不清楚 1.直接拷贝整个数据目录下的所有文件到新的机器.优点是简单.快速,只需要拷贝:缺点也很明显,在整个备份过程中新机 ...

  7. java 使用jsoup处理html字符

    依赖的jar <dependency> <groupId>org.jsoup</groupId> <artifactId>jsoup</artif ...

  8. Java读写hdfs上的avro文件

    1.通过Java往hdfs写avro文件 import java.io.File; import java.io.IOException; import java.io.OutputStream; i ...

  9. oracle数据库升级dbua操作阻塞解决方法(解决ORA-32004报错)

    操作环境 1.SuSE11sp3操作系统 2.oracle 11.2.0.3版本升级到11.2.0.4版本 问题现象   oracle 11.2.0.3版本升级到11.2.0.4版本时执行dbua命令 ...

  10. 阿里支付宝java接口

    网上关于Java支付宝接口的文章很多,都大同小异,但是具体到代码中,还是不太一样,对于以前没有调试的新手来说还是很费解的,这是通过调试认为比较有用的版本,贴在这里供大家参考. 1.从本站提交到支付宝: ...