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 ...
随机推荐
- OpenFlow Switch学习笔记(七)——Matching Fields
Matching Fields in_port=port Matches OpenFlow port port dl_vlan=vlan Matches IEEE 802.1q Virtual LAN ...
- 能不能对metronic继续封装一下呢
按照这篇文章的说法,目前metronic的层级还是较低的,只是针对Bootstrap做了很多的用例(最佳实践). 我上一个项目是用easy UI,准确地说,是经过简单封装的easy UI.用起来非常爽 ...
- 廖雪峰老师的git在线教程
我是看廖老师的网站,学习git使用的,所以在这里做做收藏,也推广推广. 该教程的类型可以换个名称<手把手看图教你用git>来概括,呵呵. 做得很用心,学起来很快.
- signtool对EXE进行签名
https://msdn.microsoft.com/zh-cn/library/9sh96ycy(VS.80).aspx .NET Framework 2.0 其他版本 文件签名工具使用 A ...
- LINQ 按多个字段排序
多字段排序 添加到 LINQ 查询结果中的Take()扩展方法用于提取前 个结果: private static void Ordering() { var racers = (from r in F ...
- jQuery获取页面及个元素高度、宽度【转】
获取浏览器显示区域(可视区域)的高度 : $(window).height(); 获取浏览器显示区域(可视区域)的宽度 : $(window).width(); 获取页面的文档高度 ...
- C++ Primer : 第十三章 : 拷贝控制之拷贝、赋值与销毁
拷贝构造函数 一个构造函数的第一个参数是自身类类型的引用,额外的参数(如果有)都有默认值,那么这个构造函数是拷贝构造函数.拷贝构造函数的第一个参数必须是一个引用类型. 合成的拷贝构造函数 在我们没 ...
- UVa 572 油田(DFS求连通块)
https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem& ...
- Oracle 删除用户和表空间
版权声明:本文为博主原创文章,未经博主允许不得转载. Oracle 使用时间长了, 新增了许多user 和tablespace. 需要清理一下 对于单个user和tablespace 来说, 可以使用 ...
- 三国游戏 2010年NOIP全国联赛普及组
题目描述 Description 小涵很喜欢电脑游戏,这些天他正在玩一个叫做<三国>的游戏. 在游戏中,小涵和计算机各执一方,组建各自的军队进行对战.游戏中共有N 位武将(N 为偶数且不小 ...