LeetCode(164) Maximum Gap
来源:互联网 发布:手机怎么制作网络问卷 编辑:程序博客网 时间:2024/05/16 17:52
题目如下:
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.
我的代码:
//48msstruct 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,这个思路还没实现。
- LeetCode(164) Maximum Gap
- [leetcode 164] Maximum Gap
- LeetCode---(164)Maximum Gap
- LeetCode #164 Maximum Gap
- leetcode 164: Maximum Gap
- LeetCode 164 Maximum Gap
- Leetcode 164 Maximum Gap
- [Leetcode] #164 Maximum Gap
- [leetcode-164]Maximum Gap(c)
- LeetCode(164)Maximum Gap
- leetcode:sort:Maximum Gap(164)
- leetcode:sort:Maximum Gap(164)
- Leetcode 164 Maximum Gap (最大差距)
- leetcode-Maximum Gap
- [LeetCode] Maximum Gap
- LeetCode Maximum Gap
- [leetCode] Maximum Gap
- leetcode 155: Maximum Gap
- hdu 3172 Virtual Friends
- Web_PHP_PHP XML DOM解析器浅谈;
- virtualBox共享文件夹的问题
- 【WOJ-1420-可见点】采用数论知识求互质对
- 学习jar命令 创建和解压jar文件包
- LeetCode(164) Maximum Gap
- 创建型设计模式
- Java爬虫搜索原理实现
- Queue 实现
- 一班里有n个学生,把各学生的姓名及学号输入并储存,再输入一个学生姓名后查找,看是否有此学生。
- Java的JDBC事务详解
- JS创建对象的探讨
- 开始自己的php之路
- Python标准库:内置函数id(object)