[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 5
and 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 ...
随机推荐
- javase基础笔记2——数据类型和面向对象
API:Application program interface 程序调用一个方法去实现一个功能 正则表达式:regex 用来匹配的 javaEE里边有三大框架 SSH struts spring ...
- Android px、dp、sp之间相互转换
dp(dip): device independent pixels(设备独立像素). 不同设备有不同的显示效果,这个和设备硬件有关,一般我们为了支持WVGA.HVGA和QVGA 推荐使用这个,不依赖 ...
- LoadRunner IP欺骗(转)
直接转了篇运用LR来实现IP欺骗的文章. http://www.cnblogs.com/fnng/archive/2013/03/02/2940284.html
- 【pom.xml 依赖】使用net.sf.json-lib-2.4-jdk15.jar所需要的其他依赖架包 以及其一直在pom.xml报错的问题
特此声明: json-lib-2.4-jdk15.jar仅它本身不够,必须如下的几个依赖架包都有才能使用!!! 首先 将.json-lib-2.4-jdk15.jar以及其相关的依赖架包的正确配置给出 ...
- 【bootstrapValidator 不验证】使用bootstrapValidator 验证效果不起作用
虽然在页面ready的时候 就绑定了验证表单 ,但是在点击提交按钮之后 依旧没有验证的效果 . 那就在提交按钮的点击事件中 添加一句话: $(document).ready( function () ...
- SpringHttpInvoker解析3-客户端实现
主要的配置文件 <bean id="httpInvokerUserService" class="org.springframework.remoting.http ...
- maven junit 单元测试插件配置
单元测试插件配置 pom.xml中增加 <dependency> <groupId>junit</groupId> <artifactId>junit& ...
- 比较全的JS checkbox全选、取消全选、删除功能代码
看下面两种实现方法: JS checkbox 方法一: 复制代码 代码如下: function checkAll() { var code_Values = document.all['code_Va ...
- VMware 锐捷 NAT模式的服务自动关闭的解决办法
之前一直搞不定VMware和锐捷的问题,校园网,你懂的. 后来发现,使用NAT模式联网时,锐捷会间隔性地终结windows系统的那个关于NAT联网的服务,你可以在计算机管理 - 服务,找到一个写有NA ...
- OpenCV 线性混合(4)
带滚动条的线性混合示例: #include "stdafx.h" #include<iostream> #include<thread> #incl ...