【题目描述】

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.

给定一个未经排序的数组,请找出其排序表中连续两个要素的最大间距。

如果数组中的要素少于 2 个,请返回 0.

【注】可以假定数组中的所有要素都是非负整数,且最大不超过 32 位整数。

【题目链接】

www.lintcode.com/en/problem/maximum-gap/

【题目解析】

假设有N个元素A到B。

那么最大差值不会小于ceiling[(B - A) / (N - 1)]

令bucket(桶)的大小len = ceiling[(B - A) / (N - 1)],则最多会有(B - A) / len + 1个桶

对于数组中的任意整数K,很容易通过算式loc = (K - A) / len找出其桶的位置,然后维护每一个桶的最大值和最小值

由于同一个桶内的元素之间的差值至多为len - 1,因此最终答案不会从同一个桶中选择。

对于每一个非空的桶p,找出下一个非空的桶q,则q.min - p.max可能就是备选答案。返回所有这些可能值中的最大值。

【参考答案】

www.jiuzhang.com/solutions/maximum-gap/

著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

Lintcode400 Maximum Gap solution 题解的更多相关文章

  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. Maximum Gap

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

  5. leetcode[164] Maximum Gap

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

  6. 【leetcode 桶排序】Maximum Gap

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

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

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

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

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

  9. LeetCode 164. Maximum Gap[翻译]

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

随机推荐

  1. cocos2d-x初探

    今天把cocos2d-x下载下来装了准备试试. 就不用windows本了,主要想做iOS游戏,所以这里用mac. 先在http://cocos2d-x.org/download下载右边的cocos,然 ...

  2. Chapter 2 User Authentication, Authorization, and Security(11):在已还原的数据库中修正登录映射错误

    原文出处:http://blog.csdn.net/dba_huangzj/article/details/39496517,专题目录:http://blog.csdn.net/dba_huangzj ...

  3. Linux IPC实践(13) --System V IPC综合实践

    实践:实现一个先进先出的共享内存shmfifo 使用消息队列即可实现消息的先进先出(FIFO), 但是使用共享内存实现消息的先进先出则更加快速; 我们首先完成C语言版本的shmfifo(基于过程调用) ...

  4. SMEM介绍

    SMEM :shared memory,是高通平台各子系统共享信息的一种机制,通过SMEM机制,PBL可以将信息传递给SBL1,SBL1可以将信息传递给RPM.LK.下面分析一个SMEM信息传递的具体 ...

  5. JSP连接access数据库

    一个用jsp连接Access数据库的代码. 要正确的使用这段代码,你需要首先在Access数据库里创建一username表,表里面创建两个字符型的字段,字段名分别为:uid,pwd,然后插入几条测试数 ...

  6. 开源视频平台:MediaCore(MediaDrop)

    MediaCore 是一个多媒体的建站系统,主要的功能包括视频.音频.YouTube集成.播客和 iTunes RSS 生成,用户可以提交各种多媒体内容. <开源中国>网站上说它是一个开源 ...

  7. Oracle Forms Services Architecture

    Oracle Forms Services Architecture Author:                         PTIAN(tianpan@gmail.com) Creation ...

  8. C++中将构造函数或析构函数定义为private

    今天面试被问到了这个单例模式常用到的技术手段,下面进行分析: 很多情况下要求当前的程序中只有一个object.例如一个程序只有一个和数据库的连接,只有一个鼠标的object.通常我们都将构造函数的声明 ...

  9. Global Financial Applications uses the following Public tables

    来自文档: Oracle  Financial Applications Technical Reference Manual  更多明细参考文档 Table Name                 ...

  10. studio grandle渠道打包

    1. Mainfest 文件中添加一个键值对,这里的value 我定义为  "UMENG_CHANNEL_VALUE"(当然实际应用中可以根据自己的需要命名),后面打包的时候会对这 ...