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的更多相关文章

  1. 【LeetCode】164. Maximum Gap (2 solutions)

    Maximum Gap Given an unsorted array, find the maximum difference between the successive elements in ...

  2. 【刷题-LeetCode】164 Maximum Gap

    Maximum Gap Given an unsorted array, find the maximum difference between the successive elements in ...

  3. 【Leetcode】164. Maximum Gap 【基数排序】

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

  4. LeetCode 笔记26 Maximum Product Subarray

    Find the contiguous subarray within an array (containing at least one number) which has the largest ...

  5. 【leetcode 桶排序】Maximum Gap

    1.题目 Given an unsorted array, find the maximum difference between the successive elements in its sor ...

  6. LeetCode 164. Maximum Gap[翻译]

    164. Maximum Gap 164. 最大间隔 Given an unsorted array, find the maximum difference between the successi ...

  7. 【leetcode】Maximum Gap

    Maximum Gap Given an unsorted array, find the maximum difference between the successive elements in ...

  8. leetcode[164] Maximum Gap

    梅西刚梅开二度,我也记一题. 在一个没排序的数组里,找出排序后的相邻数字的最大差值. 要求用线性时间和空间. 如果用nlgn的话,直接排序然后判断就可以了.so easy class Solution ...

  9. [LintCode] Maximum Gap 求最大间距

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

随机推荐

  1. 用的比较多的linux命令

    vi编辑器 :set nu  显示行号 :$       到文件最后一行 文件查找 find . -maxdepth 1 -name "@*" 这个命令意思是,查找当前目录下以@开 ...

  2. Java静态同步方法和非静态同步方法

             所有的非静态同步方法用的都是同一把锁——该实例对象本身.也就是说如果一个实例对象的非静态同步方法获取锁后,该实例对象的其他非静态同步方法必须等待获取锁的方法释放锁后才能获取锁进而执行 ...

  3. android中实现view可以滑动的六种方法

    在android开发中,经常会遇到一个view需要它能够支持滑动的需求.今天就来总结实现其滑动的六种方法.其实每一种方法的 思路都是一样的,即:监听手势触摸的坐标来实现view坐标的变化,从而实现vi ...

  4. MongoDB学习笔记——聚合操作之MapReduce

    MapReduce MongoDB中的MapReduce相当于关系数据库中的group by.使用MapReduce要实现两个函数Map和Reduce函数.Map函数调用emit(key,value) ...

  5. jsp页面img利用tomcat配置访问服务器绝对路径显示图片

    1.打开tomcat下的server.xml文件,路径\apache-tomcat-7.0.62\conf文件夹下. 2.下<host></host>加入<Context ...

  6. input输入框focus获得焦点边缘发亮

    从某个插件上摘下来的代码 <html> <head> <title> New Document </title> <style> texta ...

  7. HTTP详解2-请求、响应、缓存

    1. HTTP请求格式 做过Socket编程的人都知道,当我们设计一个通信协议时,“消息头/消息体”的分割方式是很常用的,消息头告诉对方这个消息是干什么的,消息体告诉对方怎么干.HTTP协议传输的消息 ...

  8. nginx 一二事(1) - 简单图片服务器解决方案

    最近经常有人问图片上传怎么做,有哪些方案做比较好,也看到过有关于上传图片的做法,但是都不是最好的 今天再这里简单讲一下上传图片以及图片服务器的大致理念 如果是个人项目或者企业小项目,仅仅只有十来号人使 ...

  9. Oracle中sequence的使用方法

    在Oracle数据库中,sequence等同于序列号,每次取的时候sequence会自动增加,一般会作用于需要按序列号排序的地方. 1.Create Sequence (注释:你需要有CREATE S ...

  10. grunt的使用方法,环境配置和插件安装

    虽然现在grunt的用的越来越少了,但是插件数量还是相当多的,另外grunt和gulp的使用相当相似: grunt需要安装node和npm 验证node是否安装:node -v 验证npm是否安装:n ...