【LeetCode】164. Maximum Gap (2 solutions)
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.
Credits:
Special thanks to @porker2008 for adding this problem and creating all test cases.
解法一:
先排序O(nlogn),再一次遍历,得到maxGap
虽然不满足O(n)的时间要求,但是最直观的想法。
class Solution {
public:
int maximumGap(vector<int>& nums) {
if(nums.empty() || nums.size() == )
return ;
sort(nums.begin(), nums.end());
int ret = ;
for(int i = ; i < nums.size(); i ++)
ret = max(ret, nums[i]-nums[i-]);
return ret;
}
};

解法二:为了满足O(n)复杂度,我尝试了计数排序,但是会TLE。因此使用桶排序来做。
(计数排序可以看做是桶大小为1的桶排序,但由于桶数目太多导致遍历时间过长。)
最大gap肯定是出现在后一个有效桶的min与前一个有效桶的max之间。
“有效”指的是忽略空桶。
class Solution {
public:
int maximumGap(vector<int>& nums) {
if(nums.empty() || nums.size() == )
return ;
int n = nums.size();
int minAll = *min_element(nums.begin(), nums.end());
int maxAll = *max_element(nums.begin(), nums.end());
// type conversion!!!
double gap = ((double)(maxAll - minAll)) / (n - );
// compute min and max element for each bucket
vector<int> minV(n-, INT_MAX);
vector<int> maxV(n-, INT_MIN);
for(int i = ; i < n; i ++)
{
if(nums[i] != maxAll)
{// the bktId of maxAll will fall out of bucket range
int bktId = (int)((nums[i]-minAll)/gap);
minV[bktId] = min(minV[bktId], nums[i]);
maxV[bktId] = max(maxV[bktId], nums[i]);
}
}
int ret = ;
int curMax = maxV[];
for(int i = ; i < n-; i ++)
{
if(minV[i] != INT_MAX)
{
ret = max(ret, minV[i]-curMax);
curMax = maxV[i];
}
}
ret = max(ret, maxAll-curMax);
return ret;
}
};

【LeetCode】164. Maximum Gap (2 solutions)的更多相关文章
- 【Leetcode】164. Maximum Gap 【基数排序】
Given an unsorted array, find the maximum difference between the successive elements in its sorted f ...
- 【刷题-LeetCode】164 Maximum Gap
Maximum Gap Given an unsorted array, find the maximum difference between the successive elements in ...
- 【LeetCode】53. Maximum Subarray (2 solutions)
Maximum Subarray Find the contiguous subarray within an array (containing at least one number) which ...
- 【leetcode】998. Maximum Binary Tree II
题目如下: We are given the root node of a maximum tree: a tree where every node has a value greater than ...
- 【LeetCode】895. Maximum Frequency Stack 解题报告(Python)
[LeetCode]895. Maximum Frequency Stack 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxueming ...
- 【LeetCode】718. Maximum Length of Repeated Subarray 解题报告(Python)
[LeetCode]718. Maximum Length of Repeated Subarray 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxu ...
- 【LeetCode】662. Maximum Width of Binary Tree 解题报告(Python)
[LeetCode]662. Maximum Width of Binary Tree 解题报告(Python) 标签(空格分隔): LeetCode 题目地址:https://leetcode.co ...
- 【LeetCode】104. Maximum Depth of Binary Tree (2 solutions)
Maximum Depth of Binary Tree Given a binary tree, find its maximum depth. The maximum depth is the ...
- 【LeetCode】75. Sort Colors (3 solutions)
Sort Colors Given an array with n objects colored red, white or blue, sort them so that objects of t ...
随机推荐
- DELPHI中千万别直接使用CreateThread ,建议使用BeginThread(在C++中无大问题,可是到了DELPHI中情况就不一样了)
以前在写个别程序的时候老是喜欢使用纯API编程. 在C++中无大问题,可是到了DELPHI中情况就不一样了. 当你用 DELPHI写的多线程程序莫名其妙的内存错误,特别是字符串(string)操作; ...
- Android -- Camera源码简析,启动流程
com.android.camera.Camera.java,主要的实现Activity,继承于ActivityBase. ActivityBase 在ActivityBase中执行流程: onCre ...
- 【cocos2dx开发技巧8】自定义控件-使自定义控件具有RGBA特性
转发,请保持地址:http://blog.csdn.net/stalendp/article/details/9948545 虽然CCNodeRGBA,CCLayerRGBA,sprite等提供颜色和 ...
- CPC广告反作弊
原文:http://blog.csdn.net/xwm1000/article/details/45460957 CPC广告上线也2年了,从上线以来就一直存在着作弊和反作弊的斗争,刚开始的时候流量少, ...
- 翻译记忆软件-塔多思TRADO经典教程_3
一.创建思迪术语库 1."开始>程序"中打开程序的主界面 2.按窗口内的"术语库"图标,或者"术语库"菜单"维护一个术语库数 ...
- 字段计算器VBS
ArcGIS属性表中右键可调用字段计算器.写一些简单代码可操作属性表,有VBS和Python两种. 现在要求是:如果"地块编码"为空,则将"地块编号"赋给&qu ...
- iOS后台播放音乐
iOS实现在后台播放音乐 iOS4之后就支持后台播放音频了.只需下面两步就可以实现后台播放音频操作了. 1. 在Info.plist中,添加"Required background mode ...
- cmd命令之set详解
C:\Users\Administrator>set ALLUSERSPROFILE=C:\ProgramData APPDATA=C:\Users\Administrator\AppData\ ...
- vue单文件(sfc)编译为js的流程
1.流程 2.参考文章地址 https://segmentfault.com/a/1190000012336392 3.Vue框架的parseComponent https://github.com/ ...
- 安装 Flex2packagebeta_1.994
下载文件 解压到/var/root/Media/Cydia/AutoInstall/这个路径重启手机,Cydia会自动安装好DEB文件的 patch路径 下载 /private/var/mobile/ ...