Java for LeetCode 164 Maximum Gap
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.
Credits:
Special thanks to @porker2008 for adding this problem and creating all test cases.
解题思路:
由于要用到线性时间复杂度,比较排序已经不适用了(《算法导论》P108),能够用到的有 计数排序、基数排序、堆排序,由于计数排序比较适合小范围的,基数排序最终会将顺序排好,而我们不需要那么复杂,因此,可以用桶排序,适当选择桶之间的距离,保证最大的successive distance在两桶之间即可,JAVA实现如下:
public int maximumGap(int[] nums) {
if (nums.length <= 1)
return 0;
int min = nums[0], max = nums[0];
for (int num : nums) {
min = Math.min(min, num);
max = Math.max(max, num);
}
if (max == min)
return 0;
int distance = Math.max(1, (max - min) / (nums.length - 1));
int bucket[][] = new int[(max - min) / distance + 1][2];
for (int i = 0; i < bucket.length; i++) {
bucket[i][0] = Integer.MAX_VALUE;
bucket[i][1] = -1;
}
for (int num : nums) {
int i = (num - min) / distance;
bucket[i][0] = Math.min(num, bucket[i][0]);
bucket[i][1] = Math.max(num, bucket[i][1]);
}
int maxDistance = 1, left = -1, right = -1;
for (int i = 0; i < bucket.length; i++) {
if (bucket[i][1] == -1)
continue;
if (right == -1) {
right = bucket[i][1];
continue;
}
left = bucket[i][0];
maxDistance=Math.max(maxDistance, left-right);
right=bucket[i][1];
}
return maxDistance;
}
Java for LeetCode 164 Maximum Gap的更多相关文章
- 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 寻找最大相邻数字差 --------- java
Given an unsorted array, find the maximum difference between the successive elements in its sorted f ...
- [LeetCode] 164. Maximum Gap 求最大间距
Given an unsorted array, find the maximum difference between the successive elements in its sorted f ...
- 【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】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 ...
- 164. Maximum Gap
题目: Given an unsorted array, find the maximum difference between the successive elements in its sort ...
随机推荐
- 详解Java中ArrayList、Vector、LinkedList三者的异同点
转载:https://my.oschina.net/zzw922cn/blog/491631 一.ArrayList ArrayList是一个可以处理变长数组的类型,这里不局限于"数&quo ...
- Java编程思想学习(六) 多态
1.Java语言的三大特性:继承.封装和多态. 继承:复用类的一种方法,可以简省很多代码: 封装:通过合并特征和行为来创建新的数据类型.[这种“数据类型”跟Java本身提供的8大“基本数据类型”的地位 ...
- Mysql 学习-索引的设计原则
索引的设计不合理或者缺少索引都会对数据库和应用程序的性能造成障碍.高效的索引对获的良好性能非常重要.设计索引是,应该考虑一下准则: (1)索引并非语讹夺越好,若一个表中有大量索引,不仅占用磁盘空间,而 ...
- insert 多个values
INSERT INTO `user_mail_attach` VALUES(, , , , , ), (, , , , , ); 这种比写多条insert语句效率高
- mongo(删除操作)
在使用MongoDB的时候,经常会用到MongoDB的删除操作,以下是我在使用MongoDB删除操作的总结 首先是删除用户: db.removeUser("用户名") 其次是删除数 ...
- Matlab中的括号()[]{}
Matlab中的括号()[]{} Matlab中经常会用到括号去引用某Array或者是cell的内容,但三者有什么具体区别呢?[ ] 中括号用来构建向量(Vectors)或者是矩阵(Matrices) ...
- exe4j中"this executable was created with an evaluation错误解决方法
在使用exe4j时,如果您的exe4j没有注册,在运行有exe4j转换的*.jar为*.exe的可执行文件是会提示:"this executable was created with an ...
- httpd/php/mysql的安装-1
rely: 依赖: relier, reliance , reliable : i don't think he is a reliable man . we must lay stress on ...
- PHP array_intersect() 函数
PHP Array 函数 定义和用法 array_intersect() 函数返回两个或多个数组的交集数组. 结果数组包含了所有在被比较数组中,也同时出现在所有其他参数数组中的值,键名保留不变. 注释 ...
- 使用node.js制作简易爬虫
最近看了些node.js方面的知识,就像拿它来做些什么.因为自己喜欢摄影,经常上蜂鸟网,所以寻思了一下,干脆做个简单的爬虫来扒论坛的帖子. 直接上代码吧. var sys = require(&quo ...