[LintCode] Maximum Gap 求最大间距
Given an unsorted array, find the maximum difference between the successive elements in its sorted form.
Return 0 if the array contains less than 2 elements.
Notice
You may assume all elements in the array are non-negative integers and fit in the 32-bit signed integer range.
Given [1, 9, 2, 5], the sorted form of it is[1, 2, 5, 9], the maximum gap is between 5and 9 = 4.
Sort is easy but will cost O(nlogn) time. Try to solve it in linear time and space.
LeetCode上的原题,请参见我之前的博客Maximum Gap。
class Solution {
public:
/**
* @param nums: a vector of integers
* @return: the maximum difference
*/
int maximumGap(vector<int> nums) {
if (nums.empty()) return ;
int mx = INT_MIN, mn = INT_MAX, n = nums.size();
for (int d : nums) {
mx = max(mx, d);
mn = min(mn, d);
}
int size = (mx - mn) / n + ;
int bucket_num = (mx - mn) / size + ;
vector<int> bucket_min(bucket_num, INT_MAX);
vector<int> bucket_max(bucket_num, INT_MIN);
set<int> s;
for (int d : nums) {
int idx = (d - mn) / size;
bucket_min[idx] = min(bucket_min[idx], d);
bucket_max[idx] = max(bucket_max[idx], d);
s.insert(idx);
}
int pre = , res = ;
for (int i = ; i < n; ++i) {
if (!s.count(i)) continue;
res = max(res, bucket_min[i] - bucket_max[pre]);
pre = i;
}
return res;
}
};
[LintCode] Maximum Gap 求最大间距的更多相关文章
- [LeetCode] Maximum Gap 求最大间距
Given an unsorted array, find the maximum difference between the successive elements in its sorted f ...
- [LeetCode] 164. Maximum Gap 求最大间距
Given an unsorted array, find the maximum difference between the successive elements in its sorted f ...
- LintCode "Maximum Gap"
Bucketing! A lot of details to take care. struct Bucket { Bucket() :l(-), r(-), bValid(false){}; int ...
- 由Maximum Gap,对话桶排序,基数排序和统计排序
一些非比较排序 在LeetCode中有个题目叫Maximum Gap.是求一个非排序的正数数列中按顺序排列后的最大间隔.这个题用桶排序和基数排序都能够实现.以下说一下桶排序.基数排序和计数排序这三种非 ...
- 【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 ...
随机推荐
- UML- 模型图介绍
第一类 用例图 第二类 静态图 类图 对象图 包图第三类 行为图 状态图 活动图第四类 交互图 序列图 协助图第五类 实现图 构件图 部署图 1 用例图:从用户角度描述系统功能,以及每个系统功 ...
- HR外包系统 - 工资计算-几种常见账单计算规则
01-正常工资计税 (包括同一月多地计税方式) 02-年终奖计税 (包括可分批发放,但计税总额不变) 按工资 除以月份,看落在那个计税区间,获取税率和扣除数,再用总额*税率-扣除数,要考虑当月工资如 ...
- MicroService/web Service/webAPI/RPC
[TOC] 微服务 服务拆分,利用轻量化机制(通常为HTTP源API)实现通信,复杂度可控,独立部署,技术选型灵活,容错,扩展. 康威定律的实际体现 微服务架构模式深刻影响了应用和数据库之间的关系,不 ...
- url地址中 "&" "/"等符号的转义处理(转)
URL出现了有+,空格,/,?,%,#,&,=等特殊符号的时候,可能在服务器端无法获得正确的参数值,如何是好? 解决办法:将这些字符转化成服务器可以识别的字符,对应关系如下: URL中的特殊字 ...
- 智能车学习(五)—— dac学习
一.代码共享: 1.dac.h #ifndef DAC_H_ #define DAC_H_ #include "common.h" // 2.2 DAC控制寄存器0 位定义 #de ...
- Java eclipse下 Ant build.xml实例详解
在有eclipse集成环境下ant其实不是很重要,但有些项目需要用到,另外通过eclipse来学习和理解ant是个很好的途径,所以写他demo总结下要点,希望能够帮到大家. 一.本人测试环境eclip ...
- loadrunner中切割strtok字符串
http://blog.sina.com.cn/s/blog_7ee076050102vamg.html http://www.cnblogs.com/lixiaohui-ambition/archi ...
- C语言常用知识
跳出for循环主要有以下2中方式: 1.用break语句.如: int i; for(i=0; i<10; i++) { if(i>3) // 如果i>3,跳出for循 ...
- 临时变量不能作为非const类型引用形参的实参
摘要: 非const 引用形参只能与完全同类型的非const对象关联. 具体含义为:(1)不能用const类型的对象传递给非const引用形参: ( ...
- torch7安装
按照官网进行安装即可;(http://torch.ch/docs/getting-started.html#_) # in a terminal, run the commands WITHOUT s ...