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.

Have you met this question in a real interview?

Yes
Example

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

Challenge

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 求最大间距的更多相关文章

  1. [LeetCode] Maximum Gap 求最大间距

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

  2. [LeetCode] 164. Maximum Gap 求最大间距

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

  3. LintCode "Maximum Gap"

    Bucketing! A lot of details to take care. struct Bucket { Bucket() :l(-), r(-), bValid(false){}; int ...

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

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

  5. 【leetcode】Maximum Gap

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

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

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

  7. Maximum Gap

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

  8. leetcode[164] Maximum Gap

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

  9. 【leetcode 桶排序】Maximum Gap

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

随机推荐

  1. HTTP访问的两种方式(HttpClient+HttpURLConnection)整合汇总对比(转)

    在Android上http 操作类有两种,分别是HttpClient和HttpURLConnection,其中两个类的详细介绍可以问度娘. HttpClient: HttpClient是Apache ...

  2. JAVA安装,环境变量配置

    JAVA环境变量设置 PATH %JAVA_HOME%\bin JAVA_HOME D:\ProgramFiles\Java\jdk1.6.0_10 CLASSPATH .;%JAVA_HOME%\l ...

  3. 【vijos】P1514天才的记忆

    描述 从前有个人名叫W and N and B,他有着天才般的记忆力,他珍藏了许多许多的宝藏.在他离世之后留给后人一个难题(专门考验记忆力的啊!),如果谁能轻松回答出这个问题,便可以继承他的宝藏.题目 ...

  4. slide.js使用文档

    <!doctype html> <head> <script src="js/jquery-latest.min.js"></script ...

  5. 及其简短的Splay代码

    #include <stdio.h> #include <queue> #include <algorithm> #include <stdlib.h> ...

  6. 对于JSP的调试

    在eclipse中调试JSP 我换了图片但是网页中的图片却不变化 我删了工程里的图片还是没用 看了一下Tomcat根目录..貌似也是没有的.. 最后我考虑换了HTML中图片的名字..并且更改了替换的图 ...

  7. 自己yy的fulkson最大流算法

    #include <iostream> #include <cstdio> #include <vector> using namespace std; ; //m ...

  8. SQL数据库的基本语句

    1.修改字段类型语句: alter table 表名 alter column 列名  类型 例如: alter table D  alter column no char(15): 2.从其他地方插 ...

  9. hdu3448 01背包+dfs

    题目链接:http://acm.split.hdu.edu.cn/showproblem.php?pid=3448 Description 0/1 bag problem should sound f ...

  10. AngularJS 验证

    AngularJS ng-model 指令用于绑定输入元素到模型中. 模型对象有两个属性: user 和 email. 我们使用了 ng-show指令, color:red 在邮件是 $dirty 或 ...