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实战考试题:批量创建用户和密码-看看你会么?
老男孩教育第五关实战考试题:批量创建10个用户stu01-stu10,并且设置随机8位密码,要求不能用shell的循环(例如:for,while等),只能用linux命令及管道实现. 方法1:[roo ...
- JS中比较的数值如何比较大小
<script type="text/javascript"> function check_num(){ var num=document.getElementByI ...
- SyntaxError: Use of const in strict mode.
具体报错console c:\Users\Administrator\WebstormProjects\blogtest\node_modules\connect-mongo\src\index.js ...
- 右侧导航栏(动态添加数据到list)
页面样式 <style> .scroll { position: fixed; right: 5%; top: 5em; background: #ccc; display: none; ...
- VS2012,更新补丁后的残忍--创建项目未找到与约束匹配的导出
解决方法网址:http://blog.csdn.net/jly4758/article/details/18660945
- 定时器 & 日期时间对象 & 正则
1 JavaScript 计时事件 通过使用 JavaScript,有能力做到在一个设定的时间间隔之后来执行代码,而不是在函数被调用后立即执行,这称之为计时事件. 两个关键方法是: setInterv ...
- jquery解析xml,获取xml标签名
先给一个简单的XML,结构如下 <?xml version="1.0" encoding="uft-8" ?> <msg> <ro ...
- Android开发学习——小细节注意
Android中通过Intent调用其他应用的方法(转) Android中两种序列化方式的比较Serializable和Parcelable http://www.jcodecraeer.com/a/ ...
- JFinal免费公开课更新中
价值千元的课程,免费报名学习,JFinal学院-小木 录制JFinal视频教程,JFinal核心已经周边涉及到微信小程序开发.数据库.前端实战等.
- PLSQL Developer 12 保存登录的用户名和密码
1. 登录 PLSQL Developer PLSQL Developer > Preferences 2. Preferences > Logon History > Defini ...