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 ...
随机推荐
- 博客Mac桌面编辑器-cnblogs
Mac篇 公司的机器内存只有8G,不想再大动干戈为了Windows Live Writer装个Vmware了,谷歌娘讲MarsEdit不错,那就试试用这个写个试用贴呗 就是这货了,果然是火星来的, ...
- c++11编码规范 NULL还是nullptr
0和nullptr/NULL 至于指针(地址值),根据实际选择用0.NULL还是nullptr.对使用了C++11特性的项目,选用nullptr:对于C++03项目,推荐NULL,因为它像是一个指针
- LOAD TABLE tbl_name FROM MASTER语法 SQL
用于把表的拷贝从主服务器转移到从属服务器.本语句的主要作用是调试LOAD DATA FROM MASTER.它要求用于连接主服务器的帐户拥有对主服务器的RELOAD和SUPER权限,并拥有对要载入的主 ...
- MySQL数据库高并发优化配置
在Apache, PHP, mysql的体系架构中,MySQL对于性能的影响最大,也是关键的核心部分.对于Discuz!论坛程序也是如此,MySQL的设置是否合理优化,直接 影响到论坛的速度和承载量! ...
- SWIFT语言的概览
Swift用来写iOS和OS X程序.(估计也不会支持其它屌丝系统) Swift吸取了C和Objective-C的优点,且更加强大易用. Swift可以使用现有的Cocoa和Cocoa Touch框架 ...
- click 绑定(三)防止事件冒泡
防止事件冒泡 默认情况下,Knockout允许click事件继续在更高一层的事件句柄上冒泡执行.例如,如果你的元素和父元素都绑定了click事件,那当你点击该元素的时候两个事件都会触发的.如果需要, ...
- 硬链接与软连接[转自vbird]
前言 在 Linux 底下的连结档有两种,一种是类似 Windows 的快捷方式功能的文件,可以让你快速的链接到目标文件(或目录),这种是软链接: 另一种则是透过文件系统的 inode 连结来产生新档 ...
- 前端面试库_JS部分_02
今天小编做了一个伟大的决定-----把我的初恋追回来.她在我心中一直是美好的,以前也人渣过,脑袋发热过,到了现在才真的是想有一个人陪伴着我,我与她约定晚些相见,我相信这个时间不会很久,虽然三年没有联系 ...
- feature visualization from ipython notebook
Feature visualization from ipython notebook Wang Xiao 1. install anaconda2 from: https://www.continu ...
- Intel MKL函数,如何得到相同的计算结果?【转】
在运行程序时,我们总希望多次运行的结果,是完全一致,甚至在不同的机器与不同的OS中,程序运行的结果每一位都完全相同. 事实上,程序往往很难保证做到这一点. 为什么呢? 我们先看一个简单的例子: 当程序 ...