Bucketing! A lot of details to take care.

struct Bucket
{
Bucket() :l(-), r(-), bValid(false){};
int l, r;
bool bValid;
}; class Solution {
public:
/**
* @param nums: a vector of integers
* @return: the maximum difference
*/
int maximumGap(vector<int> nums)
{
// Remove duplicates
unordered_set<int> hs(nums.begin(), nums.end());
nums.assign(hs.begin(), hs.end());
//
size_t n = nums.size();
if (n < ) return ; long long mn = *min_element(nums.begin(), nums.end());
long long mx = *max_element(nums.begin(), nums.end());
if (mn == mx) return ; // Set up buckets
vector<Bucket> bk(n);
long long interval = (mx - mn + ) / (long long)(n - );
for (auto v : nums)
{
int binx = (v - mn) / interval;
bk[binx].l = (!bk[binx].bValid) ? v : min(bk[binx].l, v);
bk[binx].r = (!bk[binx].bValid) ? v : max(bk[binx].r, v);
bk[binx].bValid = true;
} // Go check bucket by bucket
int i = , ret = ;
while (i < bk.size())
{
if (bk[i].bValid)
{
int wi = bk[i].r - bk[i].l, wo = ; int j = i + ;
while (j < bk.size() && !bk[j].bValid) j++;
if (j < bk.size())
wo = bk[j].l - bk[i].r; ret = max(ret, max(wi, wo));
i = j;
}
else
i++;
}
return ret;
}
};

LintCode "Maximum Gap"的更多相关文章

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

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

  2. 【leetcode】Maximum Gap

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

  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. Windows 10下Chrome不能启动的问题

    不能启动了 Chrome突然不能启动了,点击图标也没反应,打开任务管理器,发现点击图标后,Google Ghrome短暂地出现,随即消失. 如何解决? 解决方案 打开安装目录: C:\Program ...

  2. USB描述符概述

    在USB总线接口协议中,规定了一些标准的USB描述符,如表所示. 对于USB设备来说,有些USB描述符是必需的,例如: 设备描述符 配置描述符 字符串描述符 接口描述符 端点描述符 其余一些描述符并非 ...

  3. (基础篇)PHP字符串操作

    PHP是弱类型语言,所以其它类型的数据一般可以直接应用于字符串操作函数里,而自动转换成字符串类型,进行处理,如: echo substr("1234567", 1, 3);  和 ...

  4. 博客引索 - imsoft.cnblogs

    Java C Delphi 日常工具 图片处理 视频处理 系统工具 知识经验 奇思妙想 网站推荐

  5. SM30维护视图添加按钮

    转自http://blog.csdn.net/tsj19881202/article/details/7517232 遇到某需求,要求维护sm30的视图时,能加上排序按钮. 基本参考: http:// ...

  6. db2 字符串转换 数字

    今天使用聚合函数的时候 发现 varchar类型的是不可用的,所以呢就开始想办法解决 用到了转换函数cast(s.score as bigint) 然后有一个问题就是如果数据为空的话就会发生转换错误. ...

  7. 227. Basic Calculator II

    Implement a basic calculator to evaluate a simple expression string. The expression string contains ...

  8. 4.2springmvc校验

    1.hibernate的校验框架validation所需要jar包: 2 在applicationContext.xml中配置校验器: <!-- 校验器 --> <bean id=& ...

  9. sgu259 Printed PR    贪心

    link:http://acm.sgu.ru/problem.php?contest=0&problem=259 思路就是贪心. 首先要读懂题目,输入的方式,把样例读懂. 第一,打印的总时间一 ...

  10. 百度APIStore

    链接 http://apistore.baidu.com/ 提供了许多免费的api接口,用来做功能性的查询