【leetcode 桶排序】Maximum Gap
1、题目
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.
2、分析
题意:给定一个未排序的数组。返回其排序后的数组中 相邻元素之差 最大的值。
比方给定:[5,9,8,3,15]
排序后为:[3,5,8,9,15]。相邻元素之差最大的是15-9=6,返回6。
复杂度要求:时间空间均为O(n)。
这道题最直接的解法是,先排序,得到有序数组。然后再对相邻元素作差。找出差最大的,比方以下简短的代码:
class Solution {
public:
int maximumGap(vector<int> &num) {
if(num.size()<2) return 0;
sort(num.begin(),num.end()); //O(nlogn)
int gap=-1;
for(int i=1;i<num.size();i++){
gap=max(gap,num[i]-num[i-1]);
}
return gap;
}
};
在Leetcode上上面的代码能够AC,但其实并没有满足时间复杂度要求。由于STL函数sort()的复杂度是O(nlogn),【sort C++ reference】。
那么。线性的排序算法有哪些?计数排序、基数排序、桶排序。
以下用桶排序实现。这也是leetcode上给出的參考解法。我直接copy过来:
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.
依据上面的思路。得到代码例如以下:
class Solution {
public:
int maximumGap(vector<int> &num) {
if (num.size() < 2) return 0;
//遍历一遍。找出最大最小值
int maxNum = num[0];
int minNum = num[0];
for (int i : num) {
maxNum=max(maxNum,i);
minNum=min(minNum,i);
}
// 每一个桶的长度len,向上取整所以加+
int len = (maxNum - minNum) / num.size() + 1; //桶的个数:(maxNum - minNum) / len + 1,每一个桶里面存储属于该桶的最大值和最小值就可以,注意这里的最大最小值是局部的
vector<vector<int>> buckets((maxNum - minNum) / len + 1);
for (int x : num) {
int i = (x - minNum) / len;
if (buckets[i].empty()) {
buckets[i].reserve(2);
buckets[i].push_back(x);
buckets[i].push_back(x);
} else {
if (x < buckets[i][0]) buckets[i][0] = x;
if (x > buckets[i][1]) buckets[i][1] = x;
}
}
//gap的计算,For each non-empty buckets p, find the next non-empty buckets q, return min( q.min - p.max )
int gap = 0;
int prev = 0;
for (int i = 1; i < buckets.size(); i++) {
if (buckets[i].empty()) continue;
gap = max(gap, buckets[i][0] - buckets[prev][1]);
prev = i;
}
return gap;
}
};
【其它解法以后再更新】
【leetcode 桶排序】Maximum Gap的更多相关文章
- LeetCode 笔记28 Maximum Gap
Given an unsorted array, find the maximum difference between the successive elements in its sorted f ...
- Bucket Sort - leetcode [桶排序]
桶排序(Bucket sort)或所谓的箱排序,是一个排序算法,工作的原理是将数组分到有限数量的桶里.每个桶再个别排序(有可能再使用别的排序算法或是以递归方式继续使用桶排序进行排序).桶排序是鸽巢排序 ...
- 【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] 桶排序的特殊解,例 Sort Color
Sort Colors Given an array with n objects colored red, white or blue, sort them so that objects of t ...
- 【Leetcode】164. Maximum Gap 【基数排序】
Given an unsorted array, find the maximum difference between the successive elements in its sorted f ...
- 由Maximum Gap,对话桶排序,基数排序和统计排序
一些非比较排序 在LeetCode中有个题目叫Maximum Gap.是求一个非排序的正数数列中按顺序排列后的最大间隔.这个题用桶排序和基数排序都能够实现.以下说一下桶排序.基数排序和计数排序这三种非 ...
- 线性时间的排序算法--桶排序(以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 ...
随机推荐
- windows下安装awstats来分析apache的访问日志
一.啰嗦两句 之前在Windows下用Apache时,也曾经配置过Awstats,然后换了工作,改用Linux+nginx,渐渐把Apache忘记了.又换了工作,又得用Apache,这回版本更新到2. ...
- 【洛谷】4917:天守阁的地板【欧拉函数的应用】【lcm与gcd】【同除根号优化】
P4917 天守阁的地板 题目背景 在下克上异变中,博丽灵梦为了找到异变的源头,一路打到了天守阁 异变主谋鬼人正邪为了迎击,将天守阁反复颠倒过来,而年久失修的天守阁也因此掉下了很多块地板 异变结束后, ...
- A server is already running. Check /home/peter/stock/tmp/pids/server.pid. Exiting【Xshell 运行rails s 报错】
- Codeforces Round #228 (Div. 1) B. Fox and Minimal path 构造
B. Fox and Minimal path 题目连接: http://codeforces.com/contest/388/problem/B Description Fox Ciel wants ...
- trigger、procedure和event如何同步
最近遇到一个需求涉及存储过程,被突然问题到如何同步问题问到了,赶紧补课学习一下. 首先,先看一下trigger.procedure和event的定义都是什么? trigger: 触发器是一个被指定关联 ...
- Java_模拟comet的实现
本文没有使用任何comet服务器, 只是利用tomcat模拟实现了一下comet, 不是真正的comet哦,因为不会有这样的应用场景, 只是模拟实现, 仅供参考. 一. 需求. 实现将服务端的时间推送 ...
- HeapAlloc 和 GlobalAlloc 以及 VirtualAlloc 三者之间的关系(转)
VirtualAlloc 一次分配 1PAGE 以上的 RAM. 每次分配都是 PAGE 的整数倍. 你不会想为了分配 1 个 BYTE 的空间而浪费剩下的 4095 字节. OK, 你可以自己写算法 ...
- [Go] 复合类型(数组、切片、字典、结构体)变量的 初始化 及 注意事项
Go变量 初始化 对 复合类型(数组.切片.字典.结构体)变量的初始化是,有一些语法限制: 1.初始化表达式必须包含类型标签: 2.左花括号必须在类型尾部,不能另起一行: 3.多个成员初始值以逗号分隔 ...
- 在Powerdesigner中创建概念数据模型
点击菜单“File”---->“New Model” 点击[OK]按钮后,将进入如下的画面 系统将出现一个工具栏如下,用于在设计面板中设计模型
- Bootstrap 3之美05-排版、Button、Icon、Nav和NavBar、List、Table、Form
本篇主要包括: ■ 排版■ Button■ Icon■ Nav和NavBar■ List■ Table■ Form 排版 ● 斜体:<em>● 加粗体:<strong& ...