LeetCode 笔记28 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.
这道题题目有提示。
提示1 有线性时间复杂度解法
提示2 非负数,且强调32位整数
首先想排序的话,线性时间复杂度就那么几个解法,位图和基数排序。显然这个不能用位图,空间消耗太大。其实在看Algorithm 4th edition的时候就想,基数排序真是强大,完全可以用来做这个么。
不过当时我忘记基数排序的counting方法了。复习了一下才记起来。
public int maximumGap(int[] num) {
if (num == null || num.length <= 1) {
return 0;
}
for (int d = 0; d < 32; d++) {
int[] count = new int[3];
int[] aux = new int[num.length];
for (int i = 0; i < num.length; i++) {
count[((num[i] >> d) & 1) + 1]++;
}
for (int i = 1; i < 2; i++) {
count[i] += count[i - 1];
}
for (int i = 0; i < num.length; i++) {
aux[count[((num[i] >> d) & 1)]++] = num[i];
}
for (int i = 0; i < num.length; i++) {
num[i] = aux[i];
}
}
int maxGap = 0;
for (int i = 1; i < num.length; i++) {
if (num[i] - num[i - 1] > maxGap) {
maxGap = num[i] - num[i - 1];
}
}
return maxGap;
}
count数组的意义具体可以参考上面提到的书关于String Sort的第一部分。
然后看了下leetcode的标准答案,原来用的桶排序。这个也很强大。
假设数组中最大元素是Max, 最小元素Min,数组的长度是len,那么相邻两个数的平均间隔是D = (Max - Min)/(len - 1)。相邻两个数的最大间隔肯定大于等于这个数值。
那么我们不妨假设[Min, Max]之间所有数都可以放在紧紧排列的一个个桶中。每个桶的大小就是D。桶内元素是之间的间隔肯定不是要求的最大间隔,而是前一个桶中的最大值和后面一个桶的最小值才可能是最大间隔。我们遍历这样的值,就可以找出最大间隔。
桶的个数么,就是(Max - Min) / D + 1。
值为K的元素呢,就属于第(K - Min) / D 个桶里面了。
我们遍历一次数组,把每个桶都填上数组中的元素,同时可以求得每个桶的最大最小值。
再遍历一次桶,就求得了那个最大的间隔。
最后注意,如果len算出来是0,那么桶的个数就等于元素个数。
public int maximumGap2(int[] num) {
if (num == null || num.length <= 1) {
return 0;
}
int max = Integer.MIN_VALUE;
int min = Integer.MAX_VALUE;
for (int i = 0; i < num.length; i++) {
if (num[i] > max) {
max = num[i];
}
if (num[i] < min) {
min = num[i];
}
}
int len = (max - min) / (num.length - 1);
if (len == 0) {
len = 1;
}
int numOfBucket = (max - min) / len + 1;
Bucket[] buckets = new Bucket[numOfBucket];
for (int i = 0; i < num.length; i++) {
int idx = (num[i] - min) / len;
if (buckets[idx] == null) {
buckets[idx] = new Bucket();
}
if (num[i] > buckets[idx].max) {
buckets[idx].max = num[i];
}
if (num[i] < buckets[idx].min) {
buckets[idx].min = num[i];
}
}
int maxGap = 0;
max = -1;
for (int i = 0; i < buckets.length; i++) {
if (buckets[i] != null) {
if (max == -1) {
//pass
} else {
maxGap = Math.max(buckets[i].min - max, maxGap);
}
max = buckets[i].max;
}
}
return maxGap;
}
However,我发现桶排序做出来好像比基数排序慢也。
LeetCode 笔记28 Maximum Gap的更多相关文章
- 【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 笔记26 Maximum Product Subarray
Find the contiguous subarray within an array (containing at least one number) which has the largest ...
- 【leetcode 桶排序】Maximum Gap
1.题目 Given an unsorted array, find the maximum difference between the successive elements in its sor ...
- LeetCode 164. Maximum Gap[翻译]
164. Maximum Gap 164. 最大间隔 Given an unsorted array, find the maximum difference between the successi ...
- 【leetcode】Maximum Gap
Maximum Gap Given an unsorted array, find the maximum difference between the successive elements in ...
- leetcode[164] Maximum Gap
梅西刚梅开二度,我也记一题. 在一个没排序的数组里,找出排序后的相邻数字的最大差值. 要求用线性时间和空间. 如果用nlgn的话,直接排序然后判断就可以了.so easy class Solution ...
- [LintCode] Maximum Gap 求最大间距
Given an unsorted array, find the maximum difference between the successive elements in its sorted f ...
随机推荐
- 一些在IOS中关于JS、H5开发的网站
1.JSPatch 2.
- 《慕客网:IOS-动画入门》学习笔记
新建Cocoa Touch Class,语言是swift 然后继续为界面添加一个普通的View Controller,并且添加前面视图的静态table的转向剪头指向这个View Controller, ...
- 附带详细注释的log4net的app.config文件配置例子
<?xml version="1.0" encoding="utf-8" ?> <configuration> <configSe ...
- 《第一行代码——Android》
<第一行代码——Android> 基本信息 作者: 郭霖 丛书名: 图灵原创 出版社:人民邮电出版社 ISBN:9787115362865 上架时间:2014-7-14 出版日期:2014 ...
- asp.net的心得体会
1,gridview有点强大 2,页面的冗余代码有点多,性能略差. 3,access数据库的使用. 4,C/S端的开发,接触winform的开发 我自认为asp.net开发就像在一个别人限制好的画布上 ...
- Apache CXF自定义拦截器
为什么设计拦截器?1.为了在webservice请求过程中,能动态操作请求和响应数据,CXF设计了拦截器 拦截器分类: 1.按所处的位置分:服务器端拦截器,客户端拦截器. 2.按消息的方向分:入拦截器 ...
- Cocos2d-x解析XML文件,解决中文乱码
身处大天朝,必须学会的一项技能就是解决中文显示问题.这个字符问题还搞了我一天,以下是个人解决乱码问题的实践结果,希望可以给其他人一些帮助 读取xml文件代码: CCDictionary* messag ...
- js开发工具箱
昨天看到一位大牛的博客,里面有一篇文章“web前端开发分享-目录”,文章中提到的一个给前端er用的一个js开发工具箱.自己使用了一下,非常好用,代码压缩,代码美化,加密,解密之类基本功能都有,生成二维 ...
- docker containerd中的容器操作
containerd的中的各种操作都是通过Task来进行的,因此对于容器的create, start, delete等等操作其实都是一个个的Task而已. Task的数据结构如下所示: type Ta ...
- jmeter的使用(四)
jmeter如何调用java程序呢,下面做简单介绍.1.打开eclipse,新建项目,导入jmeter依赖的包ApacheJMeter_core.jar和ApacheJMeter_java.jar,这 ...