[LintCode] Maximum Gap 求最大间距
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.
Given [1, 9, 2, 5], the sorted form of it is[1, 2, 5, 9], the maximum gap is between 5and 9 = 4.
Sort is easy but will cost O(nlogn) time. Try to solve it in linear time and space.
LeetCode上的原题,请参见我之前的博客Maximum Gap。
class Solution {
public:
    /**
     * @param nums: a vector of integers
     * @return: the maximum difference
     */
    int maximumGap(vector<int> nums) {
        if (nums.empty()) return ;
        int mx = INT_MIN, mn = INT_MAX, n = nums.size();
        for (int d : nums) {
            mx = max(mx, d);
            mn = min(mn, d);
        }
        int size = (mx - mn) / n + ;
        int bucket_num = (mx - mn) / size + ;
        vector<int> bucket_min(bucket_num, INT_MAX);
        vector<int> bucket_max(bucket_num, INT_MIN);
        set<int> s;
        for (int d : nums) {
            int idx = (d - mn) / size;
            bucket_min[idx] = min(bucket_min[idx], d);
            bucket_max[idx] = max(bucket_max[idx], d);
            s.insert(idx);
        }
        int pre = , res = ;
        for (int i = ; i < n; ++i) {
            if (!s.count(i)) continue;
            res = max(res, bucket_min[i] - bucket_max[pre]);
            pre = i;
        }
        return res;
    }
};
[LintCode] Maximum Gap 求最大间距的更多相关文章
- [LeetCode] Maximum Gap 求最大间距
		Given an unsorted array, find the maximum difference between the successive elements in its sorted f ... 
- [LeetCode] 164. Maximum Gap 求最大间距
		Given an unsorted array, find the maximum difference between the successive elements in its sorted f ... 
- LintCode "Maximum Gap"
		Bucketing! A lot of details to take care. struct Bucket { Bucket() :l(-), r(-), bValid(false){}; int ... 
- 由Maximum Gap,对话桶排序,基数排序和统计排序
		一些非比较排序 在LeetCode中有个题目叫Maximum Gap.是求一个非排序的正数数列中按顺序排列后的最大间隔.这个题用桶排序和基数排序都能够实现.以下说一下桶排序.基数排序和计数排序这三种非 ... 
- 【leetcode】Maximum Gap
		Maximum Gap Given an unsorted array, find the maximum difference between the successive elements in ... 
- 线性时间的排序算法--桶排序(以leetcode164. Maximum Gap为例讲解)
		前言 在比较排序的算法中,快速排序的性能最佳,时间复杂度是O(N*logN).因此,在使用比较排序时,时间复杂度的下限就是O(N*logN).而桶排序的时间复杂度是O(N+C),因为它的实现并不是基于 ... 
- Maximum Gap
		Given an unsorted array, find the maximum difference between the successive elements in its sorted f ... 
- leetcode[164] Maximum Gap
		梅西刚梅开二度,我也记一题. 在一个没排序的数组里,找出排序后的相邻数字的最大差值. 要求用线性时间和空间. 如果用nlgn的话,直接排序然后判断就可以了.so easy class Solution ... 
- 【leetcode 桶排序】Maximum Gap
		1.题目 Given an unsorted array, find the maximum difference between the successive elements in its sor ... 
随机推荐
- SPI-软件开发注意事项
			01 PD ,设置数据库前一定把模板设置号,命名规则规划清楚. 
- LeetCode——Same Tree(判断两棵树是否相同)
			问题: Given two binary trees, write a function to check if they are equal or not. Two binary trees are ... 
- loj 1032 数位dp
			题目链接:http://lightoj.com/volume_showproblem.php?problem=1032 思路:数位dp, 采用记忆化搜索, dp[pos][pre][have] 表示 ... 
- linux常用命令和选项
			(1)比较两个文件. diff filename1 filename2 -y -W number; -y 并列格式输出 -W 并列格式输出时指定的列宽 (2)linux下抓包 tcpdump有三类关键 ... 
- Android Studio使用第三方类库
			导入*.jar包 新建好了Android项目,添加一个第三方已经打包好的jar文件进你项目,下面就已添加一个odata4j的一个包 在项目中添加一个libs文件 直接通过COPY/PAST 把你下载的 ... 
- Sigar介绍与使用
			Sigar是Hyperic-hq产品的基础包,是Hyperic HQ主要的数据收集组件.它用来从许多平台收集系统和处理信息. 这些平台包括:Linux, Windows, Solaris, AIX, ... 
- PHP学习之常量
			1.常量是一个简单值的标识符,该值在脚本中不能改变: 2.一个常量由英文字母,下划线,和数字组成,但数字不能作为首字母出现:(常量名中不需要加$修饰符) 3.常量在整个脚本中都可以使用: 4.设置PH ... 
- http://www.cnblogs.com/summers/p/3225375.html
			http://www.cnblogs.com/summers/p/3225375.html 
- Wordcount on YARN 一个MapReduce示例
			Hadoop YARN版本:2.2.0 关于hadoop yarn的环境搭建可以参考这篇博文:Hadoop 2.0安装以及不停集群加datanode hadoop hdfs yarn伪分布式运行,有如 ... 
- JavaScript无缝滚动
			<!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8&quo ... 
