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. ansible 小试身手

    我们安装好了ansible之后 配置了免密码登陆 现在我们可以检查一下管理主机和被管理主机的连通性 ansible   all   -m   ping 在我们的实际生产中我们倾向于使用普通用户用sud ...

  2. Oracle数据库sys为什么只能以sysdba登录

    1.我们都知道,Oracle有两个具有dba角色的用户,分别是sys与system,他们都可以以sysdba身份登录数据库.既然system具有dba角色,为什么还分配他sysoper身份? [sys ...

  3. 负margin一些奇葩的布局技巧

    copy_from_ http://www.hicss.net/i-know-you-do-not-know-the-negative-margin/ <!doctype html> &l ...

  4. STL简单应用问题

    问题: Input输入的第一行是一个整数T( 1 <= T <= 100 ),表示有几组输入数据.每组输入由4部分组成:(1)一个字典,最多包含2000个单词,每个单词一行.(2)一行字符 ...

  5. 【poj1236】 Network of Schools

    http://poj.org/problem?id=1236 (题目链接) 题意 给定一个有向图,求:1.至少要选几个顶点,才能做到从这些顶点出发,可以到达全部顶点:2.至少要加多少条边,才能使得从任 ...

  6. java连接mysql(一)

    import java.sql.*; public class MysqlTest { public static void main(String[] args) throws SQLExcepti ...

  7. Put-Me-Down项目Postmortem

    设想和目标 PMD是一款帮助低头族控制使用手机时间的APP,设想按照需求规格说明书内容实现功能,能将数据备份到服务器. 计划 初始计划我们是想将程序方面分为安卓和后台,主要是程序方面的工作.我们对项目 ...

  8. MySQL中利用外键实现级联删除、更新

    MySQL支持外键的存储引擎只有InnoDB,在创建外键的时候,要求父表必须有对应的索引,子表在创建外键的时候也会自动创建对应的索引.在创建索引的时候,可以指定在删除.更新父表时,对子表进行的相应操作 ...

  9. iOS exit(0); 直接退出程序

    exit();

  10. Javascript检测用户注册信息

    <html> <head> <title>用户注册</title> <meta http-equiv="content-type&quo ...