LeetCode(164)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.
分析
题意为,给定一组无序整数序列,求其有序形式下相邻元素的最大差值;
要求,时空复杂度均为线性。
开始还以为题意理解错了,尝试做了一下,利用库排序函数sort排序,然后一次遍历求得最大差值;时间复杂度为O(nlogn),空间复杂度为O(1);没想到AC了;
看来,题目要求如上描述很简单,考察的关键在于能否实现线性的优化也就延伸为排序算法的线性优化;
参考了别人的桶排序实现代码 ^||^… 羞愧的附上~~~
AC代码
class Solution {
public:
//方法一:STL库函数Sort排序,T(n)=O(nlogn)
int maximumGap1(vector<int>& nums) {
if (nums.empty() || nums.size() < 2)
return 0;
int len = nums.size();
sort(nums.begin(), nums.end());
int gap = 0;
for (int i = 1; i < len; ++i)
{
if (nums[i] - nums[i - 1] > gap)
gap = nums[i] - nums[i - 1];
}//for
return gap;
}
//方法二:桶排序
int maximumGap(vector<int>& nums) {
if (nums.empty() || nums.size() < 2)
return 0;
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 - 1);
// compute min and max element for each bucket
vector<int> minV(n - 1, INT_MAX);
vector<int> maxV(n - 1, INT_MIN);
for (int i = 0; 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 = 0;
int curMax = maxV[0];
for (int i = 1; i < n - 1; 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的更多相关文章
- LeetCode(152) Maximum Product Subarray
题目 Find the contiguous subarray within an array (containing at least one number) which has the large ...
- LeetCode(104) Maximum Depth of Binary Tree
题目 Given a binary tree, find its maximum depth. The maximum depth is the number of nodes along the l ...
- LeetCode(53) Maximum Subarray
题目 Find the contiguous subarray within an array (containing at least one number) which has the large ...
- LeetCode(122) Best Time to Buy and Sell Stock II
题目 Say you have an array for which the ith element is the price of a given stock on day i. Design an ...
- Leetcode(5)最长回文子串
Leetcode(4)寻找两个有序数组的中位数 [题目表述]: 给定一个字符串 s,找到 s 中 最长 的回文子串.你可以假设 s 的最大长度为 1000.' 第一种方法:未完成:利用回文子串的特点 ...
- LeetCode(275)H-Index II
题目 Follow up for H-Index: What if the citations array is sorted in ascending order? Could you optimi ...
- LeetCode(220) Contains Duplicate III
题目 Given an array of integers, find out whether there are two distinct indices i and j in the array ...
- LeetCode(154) Find Minimum in Rotated Sorted Array II
题目 Follow up for "Find Minimum in Rotated Sorted Array": What if duplicates are allowed? W ...
- LeetCode(116) Populating Next Right Pointers in Each Node
题目 Given a binary tree struct TreeLinkNode { TreeLinkNode *left; TreeLinkNode *right; TreeLinkNode * ...
随机推荐
- Linux上安装Docker,并成功部署NET Core 2.0
概述 容器,顾名思义是用来存放并容纳东西的器皿: 而容器技术伴着Docker的兴起也渐渐的映入大家的眼帘,它是一个抽象的概念,同时也是默默存在世上多年的技术,不仅能使应用程序间完全的隔离,而且还能在共 ...
- java exception "file not found or file not exist"
出现这种异常一般有两种原因,第一种就是文件真的不存在:第二种是权限问题,权限问题又分为文件本身的权限和包含它的文件夹的权限 比如 ~/aaa/bbb/ccc/ddd/eee.txt 只要 aaa , ...
- Java命令行实用工具jps和jstat 专题
在Linux或其他UNIX和类UNIX环境下,ps命令想必大家都不陌生,我相信也有不少同学写过 ps aux | grep java | grep -v grep | awk '{print $2}' ...
- 安卓H5软键盘遮挡输入框
由于安卓app内嵌入H5页面,webview对于软键盘没有处理(如果不是产品强烈要求建议不要处理这种拆东墙补西墙的问题,因为其他的手机上可能会出现已经优化软键盘的情况) 1.input下方还有多余空位 ...
- 利用Jmeter 实现Json格式接口测试
使用Jmeter模拟http请求测试接口,请求类型为json,步骤如下: 1.启动Jmeter:找到Jmeter.bat文件双击启动Jmeter. 2.在测试计划下面添加线程组:测试计划右键--添加 ...
- java 与 c#的 中 字符串比较“==”与“equals”的差异
.net中,其字符串特有的驻留机制,保证了在同一进程中,相同字符序列的字符串,只有一个实例,这样能避免相同内容的字符串重复实例化,以减少性能开销. 先来回顾一下c#中的代码: public stati ...
- 1068 乌龟棋 2010年NOIP全国联赛提高组
1068 乌龟棋 2010年NOIP全国联赛提高组 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 钻石 Diamond 题解 查看运行结果 题目描述 Descrip ...
- is 和 == 区别 编码的问题 id()函数
一丶is 和 == 的区别 == 比较的是值 is 比较的是内存地址 #字符串 a = "abc" b = "abc" print(a == b) print( ...
- Bootstrap下拉菜单相关
1.实现普通下拉菜单:.dropdown>button.dropdown-toggle[data-toggle="dropdown"]+ul.dropdown-menu; 2 ...
- mui页面间传接值例子
传值页面index.html <!DOCTYPE html><html><head> <meta charset="utf-8"> ...