题目

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;
}
};

GitHub测试程序源码

LeetCode(164)Maximum Gap的更多相关文章

  1. LeetCode(152) Maximum Product Subarray

    题目 Find the contiguous subarray within an array (containing at least one number) which has the large ...

  2. 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 ...

  3. LeetCode(53) Maximum Subarray

    题目 Find the contiguous subarray within an array (containing at least one number) which has the large ...

  4. 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 ...

  5. Leetcode(5)最长回文子串

    Leetcode(4)寻找两个有序数组的中位数 [题目表述]: 给定一个字符串 s,找到 s 中 最长 的回文子串.你可以假设 s 的最大长度为 1000.' 第一种方法:未完成:利用回文子串的特点 ...

  6. LeetCode(275)H-Index II

    题目 Follow up for H-Index: What if the citations array is sorted in ascending order? Could you optimi ...

  7. LeetCode(220) Contains Duplicate III

    题目 Given an array of integers, find out whether there are two distinct indices i and j in the array ...

  8. LeetCode(154) Find Minimum in Rotated Sorted Array II

    题目 Follow up for "Find Minimum in Rotated Sorted Array": What if duplicates are allowed? W ...

  9. LeetCode(116) Populating Next Right Pointers in Each Node

    题目 Given a binary tree struct TreeLinkNode { TreeLinkNode *left; TreeLinkNode *right; TreeLinkNode * ...

随机推荐

  1. CQRS之旅——旅程4(扩展和增强订单和注册限界上下文)

    旅程4:扩展和增强订单和注册限界上下文 进一步探索订单和注册的有界上下文. "我明白,如果一个人想看些新鲜的东西,旅行并不是没有意义的."儒勒·凡尔纳,环游世界80天 对限界上下文 ...

  2. SyntaxHighlighter

    SyntaxHighlighter uses separate syntax files called brushes to define its highlighting functionality ...

  3. Java @Validated 遇到的大坑

    我在一个Controller内,在两个方法内使用@Validated,这是两个POST方法会进入的方法,这两个方法的实体类的命名(下图红框内容)不能一样,一样的话就会导致第二个在页面显示不出来错误信息 ...

  4. JS判断android ios系统 PC端和移动端

    最近公司上线移动端,需要根据不同的系统跳转到不同的产品页面,百度后发现这一段代码很好用,不但可以判断当前是什么系统,还能知道当前浏览器是什么内核,移动端PC端都已测试无问题! var browser ...

  5. Mysql中WHERE IN,UNION 用法详解

    WHERE IN 用法 这里分两种情况来介绍 1.in 后面是记录集,如: select  *  from  table  where   uname  in(select  uname  from  ...

  6. Nmap安全扫描程序

    Nmap安全扫描程序 下载地址:https://nmap.org/download.html#windows 参考手册:https://nmap.org/man/zh/index.html#man-d ...

  7. 代码管理_Git中获取两个标签之间的变更代码行数

    操作步骤: 1.先进入git的服务器,定位到 repositories 目录 2.再定位到具体的项目目录(登录bitbucket查看项目属性可以看到项目的存放目录),如下图: 3.浏览项目的标签,命令 ...

  8. IOS制作纯色背景

    // 生成纯色背景图- (UIImage *)createPureColorImageWithColor:(UIColor *)color alpha:(CGFloat)alpha size:(CGS ...

  9. 参考消息 Android 读报

    <参考消息>是新华通讯社主办,参考消息报社编辑出版的日报,创刊于1931年,历史长达80年.<参考消息>每天及时选载世界各国(地区)通讯社.报刊及因特网上的最新消息.评论的精华 ...

  10. SQL 事物回滚

    转自https://www.cnblogs.com/delphinet/archive/2010/08/17/1801424.html 第一种:   declare   @iErrorCount    ...