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 ...
随机推荐
- 帝国cms相关调用
Loop用法:[!--temp.header--] [e:loop={6,6,0,1}] <!--标题连接/标题--> <a href="<?=$bqsr[title ...
- TCP的分层结构
TFTP(Trivial File Transfer Protocol,简单文件传输协议)是TCP/IP协议族中的一个用来在客户机与服务器之间进行简单文件传输的协议,提供不复杂.开销不大的文件传输服务 ...
- USB协议-检测设备连接与速度
在USB设备连接时,USB系统能自动检测到这个连接,并识别出其采用的数据传输速率.USB采用在D+或D-线上增加上拉电阻的方法来识别低速和全速设备. USB支持3种类型的传输速率:1.5Mb/s的低速 ...
- 2015GitWebRTC编译实录9
2015.07.20 neteq 编译通过注意不要引用tools目录里的内容 [1347/1600 ] CXX obj /webrtc/modules/audio_coding/neteq/neteq ...
- ES6 — 字符串String
ES6对字符串新增了一些函数和操作规范.下面我们来看ES6中对字符串新加的特性. 1.模版字符串 (即用反引号定义的字符串) 传统的字符串拼接通过我们使用'+'号与变量连接.例如: let name= ...
- sgu259 Printed PR 贪心
link:http://acm.sgu.ru/problem.php?contest=0&problem=259 思路就是贪心. 首先要读懂题目,输入的方式,把样例读懂. 第一,打印的总时间一 ...
- my Style
1. box-sizing语法: box-sizing : content-box || border-box || inherit 参数取值: content-box:此值为其默认值,其让元素维持W ...
- leetcode 94 Binary Tree Inorder Traversal ----- java
Given a binary tree, return the inorder traversal of its nodes' values. For example:Given binary tre ...
- JavaWeb学习记录(二十一)——国际化处理
¨国际化又称为 i18n:internationalization ¨对于软件中的菜单栏.导航条.错误提示信息,状态信息等这些固定不变的文本信息,可以把它们写在一个properties文件中,并根据不 ...
- Linux 安装rar解压工具
下载RAR安装包: http://www.rarsoft.com/download.htm 我的是CentOS 64位: wget http://www.rarsoft.com/rar/rarlinu ...