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. Html巩固

    1.html元素都支持全局属性(通用的属性)和事件属性.     a标签:定义一个超文本链接,链接到另一个超文本文件的! button:定义一个点击按钮,按钮是出发当前页面的事件.

  2. logic标签用法

    logic  <logic:iterate> <% Dog dog1=new Dog(); dog1.setAge(2); dog1.setName("xiaoming& ...

  3. JSON.NET 简单的使用

    JSON.NET(http://json.codeplex.com/)使用来将.NET中的对象转换为JSON字符串(序列化?),或者将JSON字符串转换为.NET中已有类型的对象(反序列化?) 首先为 ...

  4. 使用NHibernate实现存储库

    ORM框架不是存储库.存储库是一种架构模式,而ORM是将数据模型表示成对象模型的一种手段. 存储库可以使用ORM来协助充当领域模型和数据模型之间的中介. NHibernate允许在不损坏或只少量损坏领 ...

  5. VS2013打开项目提示此版本的应用程序不支持其项目类型(.csproj)

    命令行或者Vs自带的命令提示符输入: devenv.exe /resetskippkgs 重新打开项目即可.

  6. Oracle分页查询语句

    SELECT * FROM (SELECT A.*, ROWNUM RN FROM (SELECT * FROM (此处添加你要分页的表)) A WHERE ROWNUM <= 14000)WH ...

  7. Codeforces Round #342 (Div 2) 解题报告

    除夕夜之有生之年CF第一场 下午从奶奶家回到姥姥家,一看还有些时间,先吃点水果陪姥姥姥爷聊了会儿,再一看表,5:20....woc已经开场20分钟了...于是抓紧时间乱搞.. **A. Guest F ...

  8. 中间件、MetaQ入门学习

    目录 . 中间件技术 . MetaQ中间件 . MetaQ编程实践 1. 中间件技术 0x1: 中间件简介 中间件(Middleware)是提供系统软件和应用软件之间连接的软件,以便于软件各部件之间的 ...

  9. Erlang第三课 ---- 创建和使用module

    ----------------小技巧----------------------------- 因为这一课开始,我们要使用Erlang文件操作,所以,我们期待启动shell的时候,当前目录最好是是我 ...

  10. schemaLocation value = 'xxxxxxxxxxxx' must have even number of URI's

    这是因为没有加上Spring的版本号,加上就行了,如: http://www.springframework.org/schema/beans/spring-beans.xsd -3.2.2 http ...