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 ...
随机推荐
- iOS-编译简单静态库初探
首先声明,我写的这些网上都有更详细的内容,在这里只是写下我自己总结的一些重要内容,具体步骤如下: 事先准备:新建工程-Framework & Library - Cocoa Touch Sta ...
- ios消息
Class typedef struct objc_class *Class; struct objc_class { Class isa OBJC_ISA_AVAILABILITY; #if !__ ...
- 洛谷P1156 垃圾陷阱
动规仍然是难关啊 题目描述 卡门――农夫约翰极其珍视的一条Holsteins奶牛――已经落了到“垃圾井”中.“垃圾井”是农夫们扔垃圾的地方,它的深度为D(2<=D<=100)英尺. 卡门想 ...
- phpMyadmin /scripts/setup.php Remote Code Injection && Execution CVE-2009-1151
目录 . 漏洞描述 . 漏洞触发条件 . 漏洞影响范围 . 漏洞代码分析 . 防御方法 . 攻防思考 1. 漏洞描述 Insufficient output sanitizing when gener ...
- C++ Standard Template Library STL(undone)
目录 . C++标准模版库(Standard Template Library STL) . C++ STL容器 . C++ STL 顺序性容器 . C++ STL 关联式容器 . C++ STL 容 ...
- hihocoder #1285 智力竞赛
传送门 总结: 1.仔细读题 2.仔细分析复杂度 3.不要想当然,乱下结论 时间限制:5000ms 单点时限:1000ms 内存限制:256MB 描述 小Hi.小Ho还有被小Hi强拉来的小Z,准备组队 ...
- MyEclipse------缓冲流的使用
可以把BufferedReader和BufferedWriter称为上层流,把它们指向的字符流(Reader,Writer)称为底层流. Java采用缓存技术将上层流和底层流连接. 底层字符输入流先将 ...
- SQL Server中追踪器Trace的介绍和简单使用
一.What is Trace? 对于SQL Profiler这个工具相信大家都不是很陌生,没用过的朋友可以在SQL Server Management Studio>工具>SQL Ser ...
- 一排cell就第一个cell要点两次才响应,其他的cell都点一下就响应
一开始还以为是控件的问题,后来查了下百度,没办法谷歌不能用,结果有人说方法写错了 -(void)tableView:(UITableView *)tableView didSelectRowAtInd ...
- 如何使用网盘托管git项目
话说近年来git已经成为项目源代码管理的标准工具,有不少免费托管网站可供使用,详情参考这篇文章: http://www.cnblogs.com/zdz8207/archive/2012/05/20/2 ...