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. Maven学习遇到的第一个错误

    现在开始学习maven,我是在极客学院看的视频学习的,学习当然会遇到异常,下面是我遇到的第一个异常 [ERROR] Failed to execute goal org.apache.maven.pl ...

  2. [QualityCenter]设置工作流脚本-设置不同字段值关联不同列表

    需求:当选择A字段某个值时,设置B字段的列表值根据A字段的值来判断读取不同的列表值,如当运行省份的值已更改, 运行地区的选择列表将更改. 在脚本编辑器新建一个函数UserFuntion_Bug_Pro ...

  3. Effective Java 04 Enforce noninstantiability with a private constructor

    A class can be made noninstantiable by including a private constructor. // Noninstantiable utility c ...

  4. 《MySQL技术内幕——SQL编程》读书笔记(二)——数据类型

    对数据类型的选择将影响与数据库交互的应用程序的性能. 1.通常来说,如果一个页内可以存放尽可能多的行,那么数据库的性能就越好,因此选择一个正确的数据类型至关重要. 2.另一方面,如果在数据库中创建表时 ...

  5. PL/SQL之--包

    一.包 包是一组相关过程.函数.常量.变量.游标.异常等PL/SQL程序设计元素的组合.它类似于C++和Java中的类,其中变量相当于类中的成员变量,过程和函数相当于类中的方法.通过使用包,可以使开发 ...

  6. cocos2d-x之Box2d初试

    物理引擎:用来模拟一套物理事件的物理代码. #ifndef __HELLOWORLD_SCENE_H__ #define __HELLOWORLD_SCENE_H__ #include "c ...

  7. MyEclipse10 离线图文安装SVN插件教程

    一.下载SVN插件subclipse 1.下载 下载地址:http://subclipse.tigris.org/servlets/ProjectDocumentList?folderID=2240 ...

  8. linux移植问题汇总(一)

    linux移植问题汇总(一) 在此记录移植linux过程中出现的问题以及解决方法. 项目GitHub地址 linux3.0.80:https://github.com/numbqq/linux-3.0 ...

  9. linux网络相关配置文件

    linux系统一般来说分为两大类:1.RedHat系列:Redhat.Centos.Fedora等:2.Debian系列:Debian.Ubuntu等. linux系统中,TCP/IP网络是通过若干个 ...

  10. codeforces 711A A. Bus to Udayland(水题)

    题目链接: A. Bus to Udayland 题意: 找一对空位坐下来,水; 思路: AC代码: #include <iostream> #include <cstdio> ...