【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 ...
随机推荐
- JavaScript前世今生
JavaScript前世今生,HelloWorld与开发环境 JavaScript历史 大概在1992年,一家称作Nombas的公司开始开发一种叫做C--(C-minus-minus,简称Cmm)的嵌 ...
- go语言基础之二维数组
1.二维数组 示例: package main //必须有个main包 import "fmt" func main() { //有多少个[]就是多少维 //有多少个[]就用多少个 ...
- [leetcode]Binary Tree Preorder Traversal @ Python
原题地址:http://oj.leetcode.com/problems/binary-tree-preorder-traversal/ 题意:这题用递归比较简单.应该考察的是使用非递归实现二叉树的先 ...
- 2013年度最新最佳CSS网页设计实例
CSS在网页设计中扮演着举足轻重的角色,尤其是CSS3和HTML5的出现,让我们更加相信CSS能给我们带来无限的遐想和强有力的视觉冲击.今天我要给大家推荐几个最新CSS佳作,虽然本人没有设计头脑,但还 ...
- web 实时通信的方法总结
1.Web端即时通讯技术 即时通讯技术简单的说就是实现这样一种功能:服务器端可以即时地将数据的更新或变化反应到客户端,例如消息即时推送等功能都是通过这种技术实现的. 但是在Web中,由于浏览器的限制, ...
- Spring boot分层和基本概念
后端层次划分: 后端分包: 不同层级之间数据传输:推荐第二种 POJO与JavaBean: POJO就是简单的私有属性,加get/set方法, JavaBean,就是会做一些逻辑处理,包括接收事件,和 ...
- scala 学习笔记十 一 伴生对象
1.介绍 a.所谓伴生对象就是和某个class同名的object, 并且object 必须和class在同一个scala源文件中. b.在scala中,没有像java中的静态类,静态方法和静态成员等, ...
- Linux服务器权限管理之sudo高级应用
Sudo是允许系统管理员让普通用户执行一些或者全部的root命令的一个工具,减少了root用户的登陆和管理时间,提高了安全性,Sudo不是对shell的一个代替,它是面向每个命令的. Linux系统的 ...
- Angular报错
报错: Module 'App' is not available! You either misspelled the module name or forgot to load it. If re ...
- VMware vSphere学习之手动克隆虚拟机
VMware ESxi5.0中,在没有安装VMware vCenter server虚拟机管理器的情况下,vSphere Client是没有提供克隆选项的. 但是还是有以下方法可以通过vSphere ...