LintCode "Maximum Gap"
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"的更多相关文章
- [LintCode] Maximum Gap 求最大间距
Given an unsorted array, find the maximum difference between the successive elements in its sorted f ...
- 【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 ...
- 【LeetCode】164. Maximum Gap (2 solutions)
Maximum Gap Given an unsorted array, find the maximum difference between the successive elements in ...
- 由Maximum Gap,对话桶排序,基数排序和统计排序
一些非比较排序 在LeetCode中有个题目叫Maximum Gap.是求一个非排序的正数数列中按顺序排列后的最大间隔.这个题用桶排序和基数排序都能够实现.以下说一下桶排序.基数排序和计数排序这三种非 ...
- LeetCode 164. Maximum Gap[翻译]
164. Maximum Gap 164. 最大间隔 Given an unsorted array, find the maximum difference between the successi ...
随机推荐
- HTTP一次请求的过程
一次完整的HTTP请求所经历的7个步骤 HTTP通信机制是在一次完整的HTTP通信过程中,Web浏览器与Web服务器之间将完成下列7个步骤: 1. 建立TCP连接在HTTP工作开始之前,Web浏览器首 ...
- mysql中模糊查询的四种用法介绍
下面介绍mysql中模糊查询的四种用法: 1,%:表示任意0个或多个字符.可匹配任意类型和长度的字符,有些情况下若是中文,请使用两个百分号(%%)表示. 比如 SELECT * FROM [user] ...
- 双系统安装要点 - imsoft.cnblogs
1.用磁盘工具 取消当前激活分区,并隐藏当前激活分区2.按照普通的形式安装系统 Ghost安装和简单安装都可以3用修复启动项工具 修复之前处隐藏的系统启动项 OK,再就不会看到烦人的蓝屏了!
- iOS 协同开发出fatal error: file ‘XX-Prefix.pch’ has been modified since the precompiled header was built
在协同开发的时候,刚刚从svn下载到本地的代码,出现“fatal error: file 'XX-Prefix.pch' has been modified since the precompiled ...
- man page的介绍
假设你知道一个命令名,但不知其使用方法,此时可以求助于在线求助系统,即使用man(manual)命令,如man date. 上图为执行man date语句后的结果.<翻页查看时可按空格键,退出m ...
- Educational Codeforces Round 15 D 数学推公式
D. Road to Post Office time limit per test 1 second memory limit per test 256 megabytes input standa ...
- #梦断代码#first blood
前几天大致瞅了一眼,哇~原来不是啃代码的书,像是本小说,读起来很舒服,翻译的相当贴近生活,“这边厢......那边厢......”这类的语言很喜欢,还没看多少,对博客园比较新奇就先写个博客签个到,每天 ...
- Codeforces Round #119 (Div. 2)
A. Cut Ribbon \(f(i)\)表示长为\(i\)的布条最多可以剪几段. B. Counting Rhombi \(O(wh)\)枚举中心计算 C. Permutations 将序列一映射 ...
- IOS中打开应用实现检查更新的功能
//检查更新页面 - (void)Renew{ NSDictionary *infoDic = [[NSBundle mainBundle]infoDictionary]; ...
- 论文笔记之:Multiple Object Recognition With Visual Attention
Multiple Object Recognition With Visual Attention Google DeepMind ICRL 2015 本文提出了一种基于 attention 的用 ...