leetcode[164] Maximum Gap
梅西刚梅开二度,我也记一题。
在一个没排序的数组里,找出排序后的相邻数字的最大差值。
要求用线性时间和空间。
如果用nlgn的话,直接排序然后判断就可以了。so easy
class Solution {
public:
int maximumGap(vector<int> &num) {
if (num.size() < ) return ;
sort(num.begin(), num.end());
int maxm = -;
for (int i = ; i < num.size(); i++)
{
if (abs(num[i] - num[i-]) > maxm)
maxm = abs(num[i] - num[i-]);
}
return maxm;
}
};
但我们要的是线性时间。
其实这个思想在算法课上有讲过。用桶的思想。把数组分成几个桶,然后判断相邻桶的最大与最小之间的差值。关键是要知道每个桶的长度,已经桶的个数。
class Solution {
public:
int maximumGap(vector<int> &num) {
if (num.size() < ) return ;
int Max = num[], Min = Max, ans = ;
for (int i = ; i < num.size(); i++)
{
if (num[i] < Min)
Min = num[i];
if (num[i] > Max)
Max = num[i];
}
if (Max == Min) return ;
int bucketGap = (Max - Min)/num.size() + ; // 桶的间隔是关键
int bucketLen = (Max - Min)/bucketGap + ; // 举个 1,2,3的例子就知道了
vector<int> MinMax(, INT_MAX);
MinMax[] = INT_MIN;
vector<vector<int> > bucket(bucketLen, MinMax);
for (int i = ; i < num.size(); i++)
{
int ind = (num[i] - Min)/bucketGap;
if (num[i] < bucket[ind][])
bucket[ind][] = num[i];
if (num[i] > bucket[ind][])
bucket[ind][] = num[i];
}
int first = bucket[][], second;
for (int i = ; i < bucketLen; i++)
{
if (bucket[i][] == INT_MAX) continue;
second = bucket[i][];
int tmpmax = second - first;
ans = tmpmax > ans ? tmpmax : ans;
first = bucket[i][];
}
return ans;
}
};
最后附上官网的解法说明:
Suppose there are N elements and they range from A to B.
Then the maximum gap will be no smaller than ceiling[(B - A) / (N - 1)]
Let the length of a bucket to be len = ceiling[(B - A) / (N - 1)], then we will have at most num = (B - A) / len + 1 of bucket
for any number K in the array, we can easily find out which bucket it belongs by calculating loc = (K - A) / len and therefore maintain the maximum and minimum elements in each bucket.
Since the maximum difference between elements in the same buckets will be at most len - 1, so the final answer will not be taken from two elements in the same buckets.
For each non-empty buckets p, find the next non-empty buckets q, then q.min - p.max could be the potential answer to the question. Return the maximum of all those values.
leetcode[164] Maximum Gap的更多相关文章
- LeetCode 164. Maximum Gap[翻译]
164. Maximum Gap 164. 最大间隔 Given an unsorted array, find the maximum difference between the successi ...
- [LeetCode] 164. Maximum Gap 求最大间距
Given an unsorted array, find the maximum difference between the successive elements in its sorted f ...
- ✡ leetcode 164. Maximum Gap 寻找最大相邻数字差 --------- java
Given an unsorted array, find the maximum difference between the successive elements in its sorted f ...
- Java for LeetCode 164 Maximum Gap
Given an unsorted array, find the maximum difference between the successive elements in its sorted f ...
- 【LeetCode】164. Maximum Gap (2 solutions)
Maximum Gap Given an unsorted array, find the maximum difference between the successive elements in ...
- 【刷题-LeetCode】164 Maximum Gap
Maximum Gap Given an unsorted array, find the maximum difference between the successive elements in ...
- 【leetcode】Maximum Gap
Maximum Gap Given an unsorted array, find the maximum difference between the successive elements in ...
- 【Leetcode】164. Maximum Gap 【基数排序】
Given an unsorted array, find the maximum difference between the successive elements in its sorted f ...
- 【leetcode】Maximum Gap(hard)★
Given an unsorted array, find the maximum difference between the successive elements in its sorted f ...
随机推荐
- rm-rf 恢复过程中滥用
多DBA一定rm -rf讨厌它,也许有一天自己将数据库,以消除一个中午,然后.那么就没有一个--这种情况下,--这个不幸真的发生,你真的无药可救?不必要,有解决方法.也许你遇到不幸时,有一天.你可以用 ...
- JAVA —— 数据类型
引言:java 数据类型可分为两大类:基本数据类型和引用类型,其中基本数据类型又包括整形.浮点型.字符型和布尔型,而引用型变量与基本类型变量不同,它的值是指向内存空间的引用(地址),引用在其他语言中称 ...
- 概率图形模型(PGM)学习笔记(四)-贝叶斯网络-伯努利贝叶斯-贝叶斯多项式
之前忘记强调重要的差异:链式法则的条件概率和贝叶斯网络的链式法则之间的差异 条件概率链式法则 P\left({D,I,G,S,L} \right) = P\left( D \right)P\left( ...
- FastJson基本使用
在发展中Android的过程中.假设我们常与server联系,更新数据等,然后,json它必须是一个良好的数据格公式,但随着json.使用原生的解析也能够,可是非常不高效,所以这里介绍两种json数据 ...
- Cytoscape画图初探
Cytoscape是一个做网络图的js插件.用起来非常方便,并且非常强大.这是它的站点:点击打开链接 使用它须要导入两个文件,一个是js文件,一个是css文件.官网上下载. 这里实现了一个功能.即从后 ...
- Tigase XMPP Server在CentOS部署和配置
Tigase XMPP Server在CentOS部署与配置 作者:chszs,转载需注明.博客主页:http://blog.csdn.net/chszs 以下讲述Tigase XMPP Server ...
- java.util.Timer demo good
package timer; import java.util.Date; import java.util.Timer; import java.util.TimerTask; import org ...
- php_linux_centos6.4_安装mysql_apache_php
原文:php_linux_centos6.4_安装mysql_apache_php 原文 : http://blog.csdn.net/xiaoliouc/article/details/176395 ...
- 2014ACM/ICPC亚洲区域赛牡丹江站汇总
球队内线我也总水平,这所学校得到了前所未有的8地方,因为只有两个少年队.因此,我们13并且可以被分配到的地方,因为13和非常大的数目.据领队谁oj在之上a谁去让更多的冠军.我和tyh,sxk,doub ...
- 浅谈数据结构之KMP(串中的模式匹配算法)
KMP算法是一种模式匹配算法的改进版,其通过减少匹配的次数以及使主串不回朔来减少字符串匹配的次数,从而较少算法的相应代价,但是,事件万物是普遍归中的,KMP算法的有效性也是有一定的局限的,我将在本文的 ...