164. 最大间距

给定一个无序的数组,找出数组在排序之后,相邻元素之间最大的差值。

如果数组元素个数小于 2,则返回 0。

示例 1:

输入: [3,6,9,1]

输出: 3

解释: 排序后的数组是 [1,3,6,9], 其中相邻元素 (3,6) 和 (6,9) 之间都存在最大差值 3。

示例 2:

输入: [10]

输出: 0

解释: 数组元素个数小于 2,因此返回 0。

说明:

你可以假设数组中所有元素都是非负整数,且数值在 32 位有符号整数范围内。

请尝试在线性时间复杂度和空间复杂度的条件下解决此问题。

class Solution {
public int maximumGap(int[] nums) {
if(nums==null||nums.length<2)
return 0;
double min=nums[0];
double max=nums[0];
for(int i=0;i<nums.length;i++){ //遍历所有元素,找到最大值和最小值
min = nums[i]<min ? nums[i]:min;
max = nums[i]>max ? nums[i]:max;
}
if(min==max)
return 0;
Bucket[] buckets=new Bucket[nums.length];
int gap=(int)Math.ceil((max-min)/(nums.length-1)); //计算桶的容量
for(int i=0;i<nums.length;i++){ //遍历每个元素,计算该元素应该放置的桶的位置,将元素放入桶中,更新桶的最大值和最小值
int index=getBucketIndex((int)min,nums[i],gap);
putInBucket(buckets,nums[i],index);
}
int maxGap=buckets[0].max-buckets[0].min;
int pre=buckets[0].max;
for(int i=1;i<buckets.length;i++){ //遍历所有桶,计算最大间距(桶间间距)
if(buckets[i]!=null){
if((buckets[i].min-pre)>maxGap){
maxGap=buckets[i].min-pre;
}
pre=buckets[i].max;
}
}
return maxGap;
}
//内部类 桶
class Bucket{
int max=0;
int min=0;
boolean hasNum=false;
}
//根据元素的数值计算该元素应该在哪个桶中
public int getBucketIndex(int min,int num,int gap){
return (int)(num-min)/gap;
}
//将元素放入桶种,更新桶的最大值和最小值
public void putInBucket(Bucket[] buckets,int num,int index){
if(buckets[index]==null){
buckets[index]=new Bucket();
buckets[index].hasNum=true;
buckets[index].max=num;
buckets[index].min=num;
}
if(num>buckets[index].max)
buckets[index].max=num;
if(num<buckets[index].min)
buckets[index].min=num;
}
}

Java实现 LeetCode 164 最大间距的更多相关文章

  1. Java for LeetCode 164 Maximum Gap

    Given an unsorted array, find the maximum difference between the successive elements in its sorted f ...

  2. [LeetCode] 164. 最大间距

    题目链接 : https://leetcode-cn.com/problems/maximum-gap/ 题目描述: 给定一个无序的数组,找出数组在排序之后,相邻元素之间最大的差值. 如果数组元素个数 ...

  3. Java for LeetCode 216 Combination Sum III

    Find all possible combinations of k numbers that add up to a number n, given that only numbers from ...

  4. Java for LeetCode 214 Shortest Palindrome

    Given a string S, you are allowed to convert it to a palindrome by adding characters in front of it. ...

  5. Java for LeetCode 212 Word Search II

    Given a 2D board and a list of words from the dictionary, find all words in the board. Each word mus ...

  6. Java for LeetCode 211 Add and Search Word - Data structure design

    Design a data structure that supports the following two operations: void addWord(word)bool search(wo ...

  7. Java for LeetCode 210 Course Schedule II

    There are a total of n courses you have to take, labeled from 0 to n - 1. Some courses may have prer ...

  8. Java for LeetCode 200 Number of Islands

    Given a 2d grid map of '1's (land) and '0's (water), count the number of islands. An island is surro ...

  9. Java for LeetCode 188 Best Time to Buy and Sell Stock IV【HARD】

    Say you have an array for which the ith element is the price of a given stock on day i. Design an al ...

随机推荐

  1. tp5.1 nginx环境下url去掉index.php

    原来的地址http://xxxxxxx/index.php/admin/login/index.html 修改完的路径http://xxxxxxx/admin/login/index.html 修改. ...

  2. SunOS下root账户无法执行crontab问题

    SunOS下root账户无法执行crontab问题   直接步入正题,处理方法如下: 1.查看可执行crontab的用户: more /etc/cron.d/cron.deny 2.修改crontab ...

  3. 题解 洛谷P1562 【还是N皇后】

    原题:洛谷P1562 这个题的原理和8皇后的原理是一模一样的,就是必须要用n个皇后把每一个行填满,同时满足每一列,每一行,每一条对角线只有一个棋子.但如果按照原来的方法暴打的话只有60分(优化亲测无效 ...

  4. java读取文件内容常见几种方式

    ①随机读取文件内容 ②以行为单位读取文件,常用于读面向行的格式化文件 ③以字符为单位读取文件,常用于读文本,数字等类型的文件 ④以字节为单位读取文件,常用于读二进制文件,如图片.声音.影像等文件 pa ...

  5. 接口testing简介

    一.基础介绍 1.什么是接口 我们常说的接口一般指2种1)API:应用程序编程接口 2)GUI:图形用户界面(接口) 这里我们主要说API——接口测试   2.接口测试的目的 测试接口的正确性和稳定性 ...

  6. Flutter仿掘金点赞效果

    老孟导读:今天分享一下如何实现掘金点赞效果,这不仅仅是一篇技术文章,还是一篇解决问题思路的文章,遇到一个需求时,如何拆分需求,然后一步一步实现,这个过程比单纯的技术(此文)更有含金量. 先来看一下掘金 ...

  7. HTML5新特性 websocket(重点)--多对多聊天室

    一.html5新特性  websocket(重点)--多对多聊天室 HTTP:超文本传输协议 HTTP作用:传输网页中资源(html;css;js;image;video;..) HTTP是浏览器搬运 ...

  8. 正则+re模块知识总结

    目录 正则表达式 定义+特点 元字符 量词 贪婪匹配与非贪婪匹配 转义符 re模块 re.findall re.search re.match re.compile re.finditer re.sp ...

  9. (数据科学学习手札84)基于geopandas的空间数据分析——空间计算篇(上)

    本文示例代码.数据及文件已上传至我的Github仓库https://github.com/CNFeffery/DataScienceStudyNotes 1 简介 在本系列之前的文章中我们主要讨论了g ...

  10. POJ2991

    题目链接:https://vjudge.net/problem/POJ-2991 知识准备: 1.向量旋转公式:向量(x,y)逆时针旋转角度A,则旋转后的向量为(x*cos A-y*sin A, x* ...