题目如下:
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.
分析如下:
考虑了很多方法,估计要用到bucket sort或者radix sort。后来放狗一搜发现果然要用到bucket sort.
基本思路是这样的,把n个数放到x个桶中。我们可以构造一个合适的x,并且通过下面的证明发现,max gap只可能出现在两个桶之间,不能出现在一个桶的内部。
下面是证明,直接抄官方的解答:
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.
我的代码:
//48ms
struct Bucket {
int local_min;
int local_max;
int local_volumn;
Bucket():local_volumn(0){};
};
class Solution {
public:
int maximumGap(vector<int> &num) {
if (num.size() < 2) return 0;
if (num.size() == 2) return num[0] - num[1] > 0 ? num[0] - num[1] : num[1] - num[0];
int min = INT_MAX;
int max = INT_MIN;
int max_gap = 0;
for (int i = 0; i < num.size(); ++i) {
if (num[i] < min) min = num[i];
if (num[i] > max) max = num[i];
}
int bucket_length = (max - min) / (num.size() - 1) + 1; //maximum gap >= ceiling[(B - A) / (N - 1)]
// int bucket_length = (max - min) / num.size() + 1; //居然也能AC......
int bucket_count = (max - min) / bucket_length + 1;
vector<Bucket> bucket(bucket_count, Bucket());
for (int i = 0; i < num.size(); ++i) {
int index = (num[i] - min) / bucket_length;
if (bucket[index].local_volumn == 0) //NOTE1: 如果桶内最多只有一个元素,那么max 和 min都赋值为这个元素。
bucket[index].local_max = bucket[index].local_min = num[i];
else if (num[i] > bucket[index].local_max)
bucket[index].local_max = num[i];
else if (num[i] < bucket[index].local_min)
bucket[index].local_min = num[i];
bucket[index].local_volumn++;
}
int previous = 0; //NOTE2: 用previous来跳过空桶,第0个桶必然不是空桶,所以previous可以从0开始取值。
for (int i = 1; i < bucket.size(); ++i) {
if (bucket[i].local_volumn == 0) continue;
if (bucket[i].local_min - bucket[previous].local_max > max_gap)
max_gap = bucket[i].local_min - bucket[previous].local_max;
previous = i;
}
return max_gap;
}
};
小结扩展:
1 排序的基本思想可以扩展出很多内容。
2 经过测试发现
bucket_length = (max - min) / (num.size() - 1) + 1;
如果写为
bucket_length = (max - min) / num.size() + 1;
也可以AC。
但是从意义上来说,第1种写法我觉得才是正确的。第2种暂时还没有想到反例去推翻,但是Then the maximum gap will be no smaller than ceiling[(B - A) / (N - 1)], 所以从意义上来说我觉得不正确。
3 题目中说32-bit,我觉得强烈暗示了可以用bit方式进行radix sort,这个思路还没实现。