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

Return 0 if the array contains less than 2 elements.

Notice

You may assume all elements in the array are non-negative integers and fit in the 32-bit signed integer range.

Example

Given [1, 9, 2, 5], the sorted form of it is [1, 2, 5, 9], the maximum gap is between 5 and 9 = 4.

分析:http://bookshadow.com/weblog/2014/12/14/leetcode-maximum-gap/

假设有N个元素,最小是A, 最大是B。那么最大差值一定大于interval = (1.0 * max - min) / num.length。

我们需要num.length + 1个桶 (0 to num.length),令每个bucket(桶)的大小为interval,对于数组中的任意整数K,很容易通过算式loc = (K - A) / interval 找出其桶的位置,然后维护每一个桶的最大值和最小值.

 class Solution {
/**
* @param nums:
* an array of integers
* @return: the maximum difference
*/
public int maximumGap(int[] num) {
if (num == null || num.length < ) return ; int max = num[], min = num[];
for (int i = ; i < num.length; i++) {
max = Math.max(max, num[i]);
min = Math.min(min, num[i]);
} if (max - min <= ) return max - min; // the max gap is absolutely greater than (1.0 * max - min) / num.length
// so the interval below can guarantee the maximu gap values in two different buckets.
double interval = (1.0 * max - min) / num.length; Bucket[] buckets = new Bucket[num.length + ]; // project to (0 - n)
for (int i = ; i < buckets.length; i++) {
buckets[i] = new Bucket();
} // distribute every number to a bucket array
for (int i = ; i < num.length; i++) {
int index = (int)((num[i] - min) / interval); if (buckets[index].low == -) {
buckets[index].low = num[i];
buckets[index].high = num[i];
} else {
buckets[index].low = Math.min(buckets[index].low, num[i]);
buckets[index].high = Math.max(buckets[index].high, num[i]);
}
} // scan buckets to find maximum gap
int result = ;
int prev = buckets[].high;
for (int i = ; i < buckets.length; i++) {
if (buckets[i].low != -) {
result = Math.max(result, buckets[i].low - prev);
prev = buckets[i].high;
} } return result;
}
} class Bucket {
int low;
int high; public Bucket() {
low = -;
high = -;
}
}

Maximum Gap的更多相关文章

  1. 【leetcode】Maximum Gap

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

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

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

  3. 线性时间的排序算法--桶排序(以leetcode164. Maximum Gap为例讲解)

    前言 在比较排序的算法中,快速排序的性能最佳,时间复杂度是O(N*logN).因此,在使用比较排序时,时间复杂度的下限就是O(N*logN).而桶排序的时间复杂度是O(N+C),因为它的实现并不是基于 ...

  4. leetcode[164] Maximum Gap

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

  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 (2 solutions)

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

  7. 由Maximum Gap,对话桶排序,基数排序和统计排序

    一些非比较排序 在LeetCode中有个题目叫Maximum Gap.是求一个非排序的正数数列中按顺序排列后的最大间隔.这个题用桶排序和基数排序都能够实现.以下说一下桶排序.基数排序和计数排序这三种非 ...

  8. LeetCode 164. Maximum Gap[翻译]

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

  9. 【刷题-LeetCode】164 Maximum Gap

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

随机推荐

  1. Windows Office key 持续更新地址

    微软 Windows  Office 系列序列号,每日都有更新,上面显示的基本都可用,包括MAK及Retail Key. Windows 10   http://textuploader.com/52 ...

  2. (Struts)ActionForm类及表单数据验证

    LoginForm代码: /* * Generated by MyEclipse Struts * Template path: templates/java/JavaClass.vtl */ pac ...

  3. Android Fresco (Facebook开源的图片加载管理库)

    Fresco是Facebook开源的一个图片加载和管理库. 这里是Fresco的GitHub网址. 同类型的开源库市面有非常多,比如Picasso, Universal Image Loader, G ...

  4. 【UVA 11462】 Age Sort(基数排序)

    题 题意 给你最多2000000个数据,大小是1到99的数,让你排序输出. 分析 快排也可以过.不过这题本意是要基数排序(桶排序),就是读入年龄age, a[age]++,然后输出时,从1到99岁(看 ...

  5. WebLogic10安装图文教程

    一 WebLogic安装 1.  打开WebLogic安装程序:oepe11_wls1031.exe(我们选用的是WebLogic 10.3g).如图1-1所示: 2. 进入WebLogic安装的欢迎 ...

  6. DLUTOJ 1033 Matrix

    传送门 Time Limit: 2 Sec  Memory Limit: 128 MB Description We often use the matrix to analyze reality m ...

  7. Linux的学习路线图

    一.学习Linux的基本要求1. 掌握至少50个以上的常用命令. 2. 熟悉Gnome/KDE等X-windows桌面环境操作 . 3. 掌握.tgz..rpm等软件包的常用安装方法 4. 学习添加外 ...

  8. Redis 下载

    https://github.com/MSOpenTech/redis/releases下载 Redis伴侣Redis Desktop Manager:http://redisdesktop.com/ ...

  9. try、catch 和 throw 语句 (了解)

    C++ 异常使用 try.catch 和 throw 关键字. 引发表达式指示错误或异常情况. 可以将任何类型的对象用作引发表达式的操作数. 此对象通常用于传达有关错误的信息. 通常,应使用在标准库中 ...

  10. 初学structs2,结果类型简单示例

    一.自定义结果处理类,structs.xml中package节点下加result-types节点,在result-types节点下配置result-type的属性.然后在配置的action中的resu ...