164 Maximum Gap 最大间距
给定一个无序的数组,找出数组在排序后相邻的元素之间最大的差值。
尽量尝试在线性时间和空间复杂度情况下解决此问题。
若数组元素个数少于2,则返回0。
假定所有的元素都是非负整数且范围在32位有符号整数范围内。
详见:https://leetcode.com/problems/maximum-gap/description/
Java实现:题目要求是要线性的时间和空间,那么只能用桶排序。同一个桶里的数差值不会有不同桶间的差值大,所以找桶内最大和下一个非空桶的桶内最小进行比较即可。
class Solution {
public int maximumGap(int[] nums) {
int n=nums.length;
if (nums == null || n < 2){
return 0;
}
// 计算数组中的最大值和最小值
int min = nums[0];
int max = nums[0];
for (int num:nums) {
min = min>num?num:min;
max = max<num?num:max;
}
// the minimum possibale gap, ceiling of the integer division
int gap = (int)Math.ceil((double)(max - min)/(n - 1));
int[] bucketsMIN = new int[n - 1]; // 记录桶中的最小值
int[] bucketsMAX = new int[n - 1]; // 记录桶中最大值
Arrays.fill(bucketsMIN, Integer.MAX_VALUE);
Arrays.fill(bucketsMAX, Integer.MIN_VALUE);
// put numbers into buckets
for (int num:nums) {
if (num == min || num == max){
continue;
}
int idx = (num - min) / gap; // index of the right position in the buckets
bucketsMIN[idx] = Math.min(num, bucketsMIN[idx]);
bucketsMAX[idx] = Math.max(num, bucketsMAX[idx]);
}
// scan the buckets for the max gap
int maxGap = Integer.MIN_VALUE;
int previous = min;
for (int i = 0; i < n - 1; i++) {
if (bucketsMIN[i] == Integer.MAX_VALUE && bucketsMAX[i] == Integer.MIN_VALUE){
// 跳过空桶
continue;
}
// min value minus the previous value is the current gap
maxGap = Math.max(maxGap, bucketsMIN[i] - previous);
// update previous bucket value
previous = bucketsMAX[i];
}
maxGap = Math.max(maxGap, max - previous); // 更新最大间隔
return maxGap;
}
}
参考:https://www.cnblogs.com/grandyang/p/4234970.html
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 (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] 164. Maximum Gap 求最大间距
Given an unsorted array, find the maximum difference between the successive elements in its sorted f ...
- ✡ leetcode 164. Maximum Gap 寻找最大相邻数字差 --------- java
Given an unsorted array, find the maximum difference between the successive elements in its sorted f ...
- 164. Maximum Gap (Array; sort)
Given an unsorted array, find the maximum difference between the successive elements in its sorted f ...
- Java for LeetCode 164 Maximum Gap
Given an unsorted array, find the maximum difference between the successive elements in its sorted f ...
- 164. Maximum Gap *HARD* -- 无序数组找出排序后连续元素的最大间隔
Given an unsorted array, find the maximum difference between the successive elements in its sorted f ...
随机推荐
- xamarin.android searchview的一些用法
前言 searchview是安卓常用的搜索控件,网上有很多关于searchview都是java的,所以我参看xamaroin官网的一些demo总结一些方法. 导读 1.如何创建一个searchview ...
- SHOW PROCESSLIST Syntax
https://dev.mysql.com/doc/refman/5.7/en/show-processlist.html SHOW PROCESSLIST shows you which threa ...
- 使用Android Studio查看API文档
在使用Android Studio编码时,若要查看某个类或函数的释义, 只需将光标移动至要查看释义的代码处,然后按下Ctrl+Q,便会弹出文档描述. 然而,有时候会出现如下状况: 因为默认查看的是在线 ...
- Window XP安装Ubuntu14.04实现Samba文件共享
安装了Ubuntu14.04之后,在虚拟机设置里设置了文件共享.但在mnt文件夹下没有hgfs这个文件夹.依照网上说的去做还是不行,仅仅好放弃.改用samba实现Windows与Ubuntu文件共享. ...
- 浅谈HTTPS连接
相信很多朋友都遇到过网页被强插广告的情况,好端端一个干净的页面,动不动就被插了个屠龙宝刀点击就送的小窗口,看着就心烦.这种网页劫持强插广告的现象,在中国非常常见,往往是运营商进行HTTP劫持所造成的. ...
- Spring Boot 整合Filter
两种方法 方法一: 正常创建好Filter类,配置完成 package clc.user.filter; import javax.servlet.Filter; import javax.servl ...
- Why you shouldn’t connect your mobile application to a database
BY CRAIG CHAPMAN · PUBLISHED 2015-07-02 · UPDATED 2015-07-02 Working at Embarcadero, I frequently ...
- WIN7开机自动登录设置
WIN7开机自动登录设置 1 执行rundll32 netplwiz.dll,UsersRunDll 开始菜单中找到运行并单击运行:如下图1所示 ...
- iOS copy/retain/assign
1 深复制:内容拷贝,源对象和副本对象指的是两个不同的对象,源对象引用计数器不变,副本对象引用计数器为1 2 浅复制:指针拷贝,源对象和副本对象指的都是同一个对象,对象引用计数器+1,相当于retai ...
- ASP.NET Core:WebAppCoreRESTful
ylbtech-ASP.NET Core:WebAppCoreRESTFul 1.返回顶部 1. 2. 3. 4. 2. Controllers返回顶部 1.HomeControlle ...