题目

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. NET Core 1.1 静态文件、路由、自定义中间件、身份验证简介

    NET Core 1.1 静态文件.路由.自定义中间件.身份验证简介   概述 之前写过一篇关于<ASP.NET Core 1.0 静态文件.路由.自定义中间件.身份验证简介>的文章,主要 ...

  2. vs2012 support BI

    Microsoft SQL Server Data Tools - Business Intelligence for Visual Studio 2012 http://www.microsoft. ...

  3. 宝塔面板安装的mysql5.5用命令行kill -9后启动不了

    1.查看mysql版本方法一:status;方法二:select version(); 2.Mysql启动.停止.重启常用命令a.启动方式1.使用 service 启动:[root@localhost ...

  4. Spring中统一相同版本的api请求路径的一些思考

    Spring中统一相同版本的api请求路径的一些思考 问题场景 当我们在实际开发中,可能会遇到开发相同同版本的api, 假设相同版本的api请求路径为/v1/functionA,/v1/functio ...

  5. 如何移除网站Response Headers中的X-Powered-By信息?

    X-Powered-By是网站响应头信息其中的一个,出于安全的考虑,一般会修改或删除掉这个信息. 如果你用的node.js express框架,那么X-Powered-By就会显示Express.如果 ...

  6. Python+Selenium之断言对应的元素是否获取以及基础知识回顾

    # coding=utf-8 from selenium import webdriver driver = webdriver.Firefox() driver.maximize_window () ...

  7. c# winform 关于DataGridView的一些操作

    转自:http://heisetoufa.iteye.com/blog/405317 设置字段名 设置字段值 设定单元格表示 Error图标 设定当前单元格 取得当前单元格内容 取得当前单元格的列 I ...

  8. 【Python图像特征的音乐序列生成】数据集制作的一些tricks

    关于数据集的制作,我决定去掉很多不必要的东西,比如和弦,于是我选择了melody部分的旋律. 有了midi文件,我现在要abc序列,所以我要通过midi2abc转换一下文件. 批处理程序效果如下: 文 ...

  9. MovieReview—A dog's purpose(一只狗的使命)

    Be Here Now                                                             A dog in the movie was reinc ...

  10. python中yield的用法详解

    首先我要吐槽一下,看程序的过程中遇见了yield这个关键字,然后百度的时候,发现没有一个能简单的让我懂的,讲起来真TM的都是头头是道,什么参数,什么传递的,还口口声声说自己的教程是最简单的,最浅显易懂 ...