LeetCode 笔记28 Maximum Gap
Given an unsorted array, find the maximum difference between the successive elements in its sorted form.
Try to solve it in linear time/space.
Return 0 if the array contains less than 2 elements.
You may assume all elements in the array are non-negative integers and fit in the 32-bit signed integer range.
这道题题目有提示。
提示1 有线性时间复杂度解法
提示2 非负数,且强调32位整数
首先想排序的话,线性时间复杂度就那么几个解法,位图和基数排序。显然这个不能用位图,空间消耗太大。其实在看Algorithm 4th edition的时候就想,基数排序真是强大,完全可以用来做这个么。
不过当时我忘记基数排序的counting方法了。复习了一下才记起来。
public int maximumGap(int[] num) {
if (num == null || num.length <= 1) {
return 0;
}
for (int d = 0; d < 32; d++) {
int[] count = new int[3];
int[] aux = new int[num.length];
for (int i = 0; i < num.length; i++) {
count[((num[i] >> d) & 1) + 1]++;
}
for (int i = 1; i < 2; i++) {
count[i] += count[i - 1];
}
for (int i = 0; i < num.length; i++) {
aux[count[((num[i] >> d) & 1)]++] = num[i];
}
for (int i = 0; i < num.length; i++) {
num[i] = aux[i];
}
}
int maxGap = 0;
for (int i = 1; i < num.length; i++) {
if (num[i] - num[i - 1] > maxGap) {
maxGap = num[i] - num[i - 1];
}
}
return maxGap;
}
count数组的意义具体可以参考上面提到的书关于String Sort的第一部分。
然后看了下leetcode的标准答案,原来用的桶排序。这个也很强大。
假设数组中最大元素是Max, 最小元素Min,数组的长度是len,那么相邻两个数的平均间隔是D = (Max - Min)/(len - 1)。相邻两个数的最大间隔肯定大于等于这个数值。
那么我们不妨假设[Min, Max]之间所有数都可以放在紧紧排列的一个个桶中。每个桶的大小就是D。桶内元素是之间的间隔肯定不是要求的最大间隔,而是前一个桶中的最大值和后面一个桶的最小值才可能是最大间隔。我们遍历这样的值,就可以找出最大间隔。
桶的个数么,就是(Max - Min) / D + 1。
值为K的元素呢,就属于第(K - Min) / D 个桶里面了。
我们遍历一次数组,把每个桶都填上数组中的元素,同时可以求得每个桶的最大最小值。
再遍历一次桶,就求得了那个最大的间隔。
最后注意,如果len算出来是0,那么桶的个数就等于元素个数。
public int maximumGap2(int[] num) {
if (num == null || num.length <= 1) {
return 0;
}
int max = Integer.MIN_VALUE;
int min = Integer.MAX_VALUE;
for (int i = 0; i < num.length; i++) {
if (num[i] > max) {
max = num[i];
}
if (num[i] < min) {
min = num[i];
}
}
int len = (max - min) / (num.length - 1);
if (len == 0) {
len = 1;
}
int numOfBucket = (max - min) / len + 1;
Bucket[] buckets = new Bucket[numOfBucket];
for (int i = 0; i < num.length; i++) {
int idx = (num[i] - min) / len;
if (buckets[idx] == null) {
buckets[idx] = new Bucket();
}
if (num[i] > buckets[idx].max) {
buckets[idx].max = num[i];
}
if (num[i] < buckets[idx].min) {
buckets[idx].min = num[i];
}
}
int maxGap = 0;
max = -1;
for (int i = 0; i < buckets.length; i++) {
if (buckets[i] != null) {
if (max == -1) {
//pass
} else {
maxGap = Math.max(buckets[i].min - max, maxGap);
}
max = buckets[i].max;
}
}
return maxGap;
}
However,我发现桶排序做出来好像比基数排序慢也。
LeetCode 笔记28 Maximum Gap的更多相关文章
- 【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】164. Maximum Gap 【基数排序】
Given an unsorted array, find the maximum difference between the successive elements in its sorted f ...
- LeetCode 笔记26 Maximum Product Subarray
Find the contiguous subarray within an array (containing at least one number) which has the largest ...
- 【leetcode 桶排序】Maximum Gap
1.题目 Given an unsorted array, find the maximum difference between the successive elements in its sor ...
- LeetCode 164. Maximum Gap[翻译]
164. Maximum Gap 164. 最大间隔 Given an unsorted array, find the maximum difference between the successi ...
- 【leetcode】Maximum Gap
Maximum Gap Given an unsorted array, find the maximum difference between the successive elements in ...
- leetcode[164] Maximum Gap
梅西刚梅开二度,我也记一题. 在一个没排序的数组里,找出排序后的相邻数字的最大差值. 要求用线性时间和空间. 如果用nlgn的话,直接排序然后判断就可以了.so easy class Solution ...
- [LintCode] Maximum Gap 求最大间距
Given an unsorted array, find the maximum difference between the successive elements in its sorted f ...
随机推荐
- Java部分总结图片版2(已加上原图链接!!!)
Java部分总结图片版2(加上原图链接)
- UNIX/Linux下C语言的学习路线
一.工具篇 “公欲善其事,必先利其器”.编程是一门实践性很强的工作,在你以后的学习或工作中,你将常常会与以下工具打交道, 下面列出学习C语言编程常常用到的软件和工具. 1.操作系统 在UNIX或 ...
- 这些git技能够你用一年了
用git有一年了,下面是我这一年来的git使用总结,覆盖了日常使用中绝大多数的场景.嗯,至少是够用一年了,整理出来分享给大家,不明白的地方可以回复交流. git设置关闭自动换行 git config ...
- android 5.X之使用Palette
这几天为了学些android5.0版本sdk的新特性,折腾了好久.AndroidStudio被我反复的安装又卸载又安装,在eclipse和AndroidStudio 之间来回折腾.没想到sdk升级到5 ...
- Effective Java 73 Avoid thread groups
Thread groups were originally envisioned as a mechanism for isolating applets for security purposes. ...
- windows 下my.ini的配置优化
线上有若干WIN环境下的MySQL,需要进行优化配置,列出以下参数方便查阅 # For advice on how to change settings please see # http://dev ...
- PPT制作教程:如何制作ppt
PowerPoint(PPT)是专门用于制作演示文稿(俗称幻灯片).广泛运用于各种会议.产品演示.学校教学等.学会如何制作ppt,成为提升工作效 率的好帮手.PPT包含有很多的功能,我们可以根据个人喜 ...
- 烂泥:KVM虚拟机windows系统增加硬盘
本文由秀依林枫提供友情赞助,首发于烂泥行天下. 前一篇文章介绍了有关linux系统添加硬盘的方法,这次我们来介绍有关windows系统添加的相关步骤. 其实linux和windows添加的硬盘的方法都 ...
- 第九章:四大组件之Broadcast Receiver
第九章:四大组件之Broadcast Receiver 一.广播的功能和特征 广播的生命周期很短,经过调用对象-->实现onReceive-->结束,整个过程就结束了.从实现的复杂度和 ...
- 探索 OpenStack 之(9):深入块存储服务Cinder (功能篇)
继研究了Neutron之后,继续Nova的外围研究之旅.本站是研究块存储服务Cinder. 0.验证环境 环境包括: 1.一个controller节点,运行nova-api, nova-schedul ...