✡ leetcode 164. Maximum Gap 寻找最大相邻数字差 --------- java
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.
给定一个未排序的数组,然后找出排序之后的数组中,相邻数字的最大差。
1、桶排序
public class Solution {
public int maximumGap(int[] nums) {
int len = nums.length;
if (len < 2){
return 0;
}
int max = nums[0];
int min = nums[0];
for (int num : nums){
if (max < num){
max = num;
} else if ( min > num){
min = num;
}
}
int gap = (max-min)/(len-1);
if( gap == 0){
gap = 1;
}
int size = (max - min) / gap + 1;
int[] gapMax = new int[size];
int[] gapMin = new int[size];
for (int num : nums){
int pos = (num - min)/gap;
if (gapMax[pos] < num){
gapMax[pos] = num;
}
if (gapMin[pos] == 0 || gapMin[pos] > num){
gapMin[pos] = num;
}
}
int start = min;
int end = gapMax[0];
int result = end - start;
for (int i = 0; i < size - 1; i++){
start = gapMax[i] == 0 ? start : gapMax[i];
end = gapMin[i+1];
if (result < (end - start)){
result = end - start;
}
}
if (gapMax[size - 1] == 0 && end - start > result){
result = end - start;
} else if (gapMax[size - 1] != 0 && end - gapMax[size - 1] > result){
result = end - gapMax[size - 1];
}
return result;
}
}
✡ leetcode 164. Maximum Gap 寻找最大相邻数字差 --------- java的更多相关文章
- 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 求最大间距
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 ...
- 【刷题-LeetCode】164 Maximum Gap
Maximum Gap Given an unsorted array, find the maximum difference between the successive elements in ...
- 【LeetCode】164. Maximum Gap (2 solutions)
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】Maximum Gap(hard)★
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 ...
随机推荐
- Python脚本开头两行的:#!/usr/bin/python和# -*- coding: utf-8 -*-的作用
#!/usr/bin/Python指定用什么解释器运行脚本以及解释器所在的位置 # -*- coding: utf-8 -*-用来指定文件编码为utf-8的 估计有不少人注意过一些python脚本开头 ...
- CentOS 6 使用 yum 安装MongoDB及服务器端配置
安装MongoDB的方法有很多种,可以源代码安装,在Centos也可以用yum源安装的方法.由于MongoDB更新得比较快,我比较喜欢用yum源安装的方法.64位Centos下的安装步骤如下: 1.准 ...
- MPICH3 配置安装问题列表
问题列表 1: configure: error: F90 and F90FLAGS are replaced by FC and FCFLAGS respectively in this confi ...
- setInterval和setTimeout的区别
setInterval会每隔指定的毫秒数后反复执行指定代码. setTimeout只会在指定的毫秒数后执行一次指定代码. setInterval的用法: // 创建(创建后即开始计时) var int ...
- WebView注入Java对象注意事项
在android4.2以前,注入步骤如下: webview.getSetting().setJavaScriptEnable(true); class JsObject { public String ...
- public,protected,private辨析
一直没有很清楚理解这三个修饰权限的区别,今天终于搞明白了,现总结如下: private:最严格的一个,子类无法继承,只有本类内部内访问,在其余类及子类中通过 "类名.方法" 去调用 ...
- Windows Store App 用户库文件夹操作
上面介绍了与用户库文件有关的操作,包括创建.读写等,下面将介绍与用户库文件夹相关的操作. 与文件操作一样,想要对用户库文件夹进行操作,需要首先获取用户库的相应位置,获取的方法上面已经介绍过了,这里不再 ...
- UE3植被工具-支持刷Actor)
[目标] 植被工具-刷Actor [思路] 1 添加类型FFoliageMeshInfo.AddInstance 的函数 2 添加Instance就直接SpawnActor 3 类结构 4 修改的函数 ...
- Leetcode 1 two sum 难度:0
https://leetcode.com/problems/two-sum/ class Solution { public: vector<int> twoSum(vector<i ...
- HDU 4940 Destroy Transportation system(2014 Multi-University Training Contest 7)
思路:无源汇有上下界可行流判定, 原来每条边转化成 下界为D 上界为 D+B ,判断是否存在可行流即可. 为什么呢? 如果存在可行流 那么说明对于任意的 S 集合流出的肯定等于 流入的, ...