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.
public class Solution { public int maximumGap(int[] nums) { if (nums == null || nums.length < 2) { return 0; } int min = Integer.MAX_VALUE; int max = Integer.MIN_VALUE; for (int i = 0; i < nums.length; i++) { min = Math.min(min, nums[i]); max = Math.max(max, nums[i]); } if (min == max) { return 0; } boolean[] hasNum = new boolean[nums.length+1]; int[] maxs = new int[nums.length+1]; int[] mins = new int[nums.length+1]; int bid = 0; for (int i = 0; i < nums.length; i++) { bid = bucket(nums[i], nums.length, min, max); mins[bid] = hasNum[bid] ? Math.min(mins[bid], nums[i]) : nums[i]; maxs[bid] = hasNum[bid] ? Math.max(maxs[bid], nums[i]) : nums[i]; hasNum[bid] = true; } int res = 0; int lastMax = 0; int i = 0; while (i <= nums.length) { if (hasNum[i++]) { lastMax = maxs[i-1]; break; } } for (; i <= nums.length; i++) { if (hasNum[i]) { res = Math.max(res, mins[i]-lastMax); lastMax = maxs[i]; } } return res; } private int bucket(long num, long length, long min, long max) { // TODO Auto-generated method stub return (int) ((num - min) * length / (max - min)); } }
相关推荐
java java_leetcode题解之Maximum Gap.java
二阶分圆多项式最大间隔的注记,纪春岗,张彬,设$p<q$为奇素数,令$g(Phi_{pq})$表示分圆多项式$Phi_{pq}(x)$的连续非零系数的最大间隔。本文给出了Hong等人[J. Number Theory, 132 (2012), pp....
假设数组长度为N,准备N+1个桶,把max放在N+1号桶,nums中在[min,max)范围上的数放在1~N号桶中,对于1 ~ N号桶的每一个桶来说,负责的区间
javascript js_leetcode题解之164-maximum-gap.js
5. **最大间隔(Maximum gap)** 在线设备轨道末端之间的最大允许间隔为0.375英寸,确保平稳过渡。 6. **导入长度(Lead-in)** 输送机轨道末端的最小导入长度为0.125英寸,有助于引导板材进入下一设备。 ####...
318| [Maximum Product of Word Lengths](https://leetcode.com/problems/maximum-product-of-word-lengths/) | [C++](./C++/maximum-product-of-word-lengths.cpp) [Python](./Python/maximum-product-of-word-...
Orao差距计算任务 JSON文件解析器可帮助计算... maximum gap 。 如果我们至少有一条不良线,我们的差距为1,如果有10条不良线排序,我们的差距为10。我们需要计算最大差距 average gap -与2相同,但所有差距均为平均值
Allow user to define maximum gap for OSB/GB Allow user to define location for all OSB/GB files to be kept Allow user to browse for path as well as directly type in path Provide a very simple text ...
Maximum Gap: Something about largest-rectangle-in-histogram: 最长递增子序列: 最长公共自序列: Something about: best time to buy and sell stack: 单链表中的环,两个单链表的公共点。 populating-next-...
164-Maximum Gap(bucket sort and pighole), 147-Insertion Sort List, 148-Sort List(Mergesort), 215-Kth Largest element in an Array(Heap Sort), HARD: 218-The Skyline Problem(Mergesort) ) 动态规划 HARD: ...
Gap,如果对排序时间复杂度要求为O(N),考虑空换时的方式,典型代表为桶排序 2019/07/10 新增 214. Shortest Palindrome,为了满足时间复杂度要求,这里求回文串使用Manacher算法 2019/07/11 新增 224. Basic Calculator,...
系统发育分析通常使用各种算法,如最大简约法(Maximum Parsimony)、最大似然法(Maximum Likelihood)和贝叶斯方法(Bayesian inference),来从序列比对中推断进化树。这些算法会考虑碱基替换、插入和删除事件,...
As the thickness decreases, a red shift of band gap is observed, and there is a maximum of red shift. The values of the maximum red shifts are dependent on the standard deviations of micro-spheres. ...
Based on the theory of nonresonant Fresnel phase matching (FPM),tuning characteristics of second-harmonic generation (SHG) wave,the full-width at half-maximum (FWHM) acceptance bandwidth for total ...
%最大遗传代数(Maximum number of generations) GGAP=0.95; %代沟(Generation gap) trace=zeros(MAXGEN,2); %寻优结果的初始值 BaseV=crtbase([6 3],[3 5]); 4.注意事项:注意MATLAB左侧当前文件夹路径,必须是...
- 可定义FOM静态标准,包括minimum、average、maximum。 3. **CoverageTime** - 分析栅格点被覆盖的时间总和。 - 仅显示静态图形。 - 报告形式包括: - RealTime(实时) - PercentageofTimecovered(覆盖...
PIPEGAPSIZE = 100 # gap between upper and lower part of pipe 管道上下之间的间隙 BASEY = SCREENHEIGHT * 0.79 #base那个条条所在的高度 注意以左上角为坐标起始点 所以这个高度是往下为正 # image, sound and ...
如果要更改 conky 窗口的位置,可以在“gap_x”和“gap_y”处进行。 minimum_size 和 maximum_width 可以改变你的 conky 窗口的大小。 根据您的需要更改 CPU 参数; 例如,如果您只有两个内核,请移除 CPU2 和 CPU3...
在数据包的传输特性方面,帧间隔(Interframe gap)指的是帧与帧之间的时间间隔;突发(Burst)指的是短时间内数据量的急剧增加;突发量(Burst size)是指突发期间的数据包数量;突发间隔(Interburst gap)是两个...