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.

 
 
如果不考虑题目中要求的Linear time/space,实际上只需要先sort,然后找到最大的就可以了。
 class Solution {
public:
int maximumGap(vector<int> &num) { if(num.size()<)
return ; sort(num.begin(),num.end());
int max=; for(int i=;i<num.size()-;i++)
{
if(num[i+]-num[i]>max)
max=num[i+]-num[i];
}
return max;
}
};
 
符合题意的方法:
 
由于num中的数字肯定在[min,max]区间内,所以根据抽屉原理,假设num中有n个数字,则最大的gap必然要大于dis=(max-min)/(n-1),所以我们可以把num所在的范围分成等间隔的区间,相邻区间内的元素之间的最大差值,即为要寻找的gap
 
 class Solution {
public:
int maximumGap(vector<int> &num) { if(num.size()<) return ;
if(num.size()==) return abs(num[]-num[]); int n=num.size();
int min,max,i; min=max=num[]; for(i=;i<num.size();i++)
{
if(min>num[i]) min=num[i];
if(max<num[i]) max=num[i];
} // 找到区间间隔
//注意此处,也可以写成(max-min)/n+1,此时就不需要num.size()==2的边界条件了
int dis=(max-min)/(n-)+; vector<vector<int> > bucket((max-min)/dis+); for(i=;i<n;i++)
{
int x=num[i];
int index=(x-min)/dis;
//把元素放入不同的区间中
if(bucket[index].empty())
{
bucket[index].reserve();
bucket[index].push_back(x);
bucket[index].push_back(x);
}
else
{
if(bucket[index][]>x) bucket[index][]=x;
if(bucket[index][]<x) bucket[index][]=x;
}
} int pre=;
int gap=; //在相邻的区间中(区间内有元素的相邻区间)寻找最大的gap
for(i=;i<bucket.size();i++)
{
if(bucket[i].empty()) continue; int tmp=bucket[i][]-bucket[pre][];
if(gap<tmp) gap=tmp;
pre=i;
}
return gap;
}
};

【leetcode】Maximum Gap的更多相关文章

  1. 【leetcode】Maximum Gap(hard)★

    Given an unsorted array, find the maximum difference between the successive elements in its sorted f ...

  2. 【leetcode】Maximum Subarray (53)

    1.   Maximum Subarray (#53) Find the contiguous subarray within an array (containing at least one nu ...

  3. 【leetcode】Maximum Subarray

    Maximum Subarray Find the contiguous subarray within an array (containing at least one number) which ...

  4. 【LeetCode】Maximum Product Subarray 求连续子数组使其乘积最大

    Add Date 2014-09-23 Maximum Product Subarray Find the contiguous subarray within an array (containin ...

  5. 【LeetCode】Maximum Depth of Binary Tree

    http://oj.leetcode.com/problems/maximum-depth-of-binary-tree/ public class Solution { public int max ...

  6. 【LeetCode】Maximum Depth of Binary Tree(二叉树的最大深度)

    这道题是LeetCode里的第104道题. 给出题目: 给定一个二叉树,找出其最大深度. 二叉树的深度为根节点到最远叶子节点的最长路径上的节点数. 说明: 叶子节点是指没有子节点的节点. 示例: 给定 ...

  7. 【LeetCode】Maximum Subarray(最大子序和)

    这道题是LeetCode里的第53道题. 题目描述: 给定一个整数数组 nums ,找到一个具有最大和的连续子数组(子数组最少包含一个元素),返回其最大和. 示例: 输入: [-2,1,-3,4,-1 ...

  8. 【leetcode】Maximum Product of Word Lengths

    Given a string array words, find the maximum value of length(word[i]) * length(word[j]) where the tw ...

  9. 【Leetcode】Maximum Product Subarray

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

随机推荐

  1. jQuery里$(this)和this的区别在哪?

    $(this)是jquery对象,能调用jquery的方法,例如click(), keyup().而this,则是html元素对象,能调用元素属性,例如this.id,this.value.例如假设已 ...

  2. input实时监听value change

    oninput ie9+,otheronpropertychange  ie6-8 js使用 var el = docuemnt.getElementById('id'); if('oninput' ...

  3. onscroll事件的浏览器支持

    window和普通div对象的scroll事件,被全部浏览器支持,其他元素的scroll事件,仅部分浏览器支持,如下图 出处: http://w3help.org/zh-cn/causes/SD901 ...

  4. word中那些重要但是被人忽略的快捷键和长word文档的跳转

    重复上一次操作: F4, 这个太重要了,比如你在做一次很复杂的操作, 下一次又要这样操作时就很有用! 如设置 文字的 段落背景/ 底纹颜色!时要多次设置这个时就 非常有用! 段落缩进:ctrl+M : ...

  5. 全屏背景:15个jQuery插件实现全屏背景图像或媒体

    动态网站通常利用背景图像或预加载屏幕,以保证所有资源都加载到页面上,在浏览器中充分呈现.现在很多网站都炫耀自己的图像作为背景图像全屏背景,追溯到旧的Flash网站却用自己的方式在HTML资源重布局. ...

  6. 利用Node.js对某智能家居服务器重构

    原文摘自我的前端博客,欢迎大家来访问 http://www.hacke2.cn 之前负责过一个智能家居项目的开发,外包重庆一家公司的,我们主要开发服务器监控和集群版管理. 移动端和机顶盒的远程通信是用 ...

  7. 大熊君大话NodeJS之开篇------Why NodeJS(将Javascript进行到底)

    一,开篇分析 大家好啊,大熊君又来啦(*^__^*) 嘻嘻……,之前我写过一系列关于JS(OOP与设计模式)方面的文章,反响还好,其实这也是对我本人最大的鼓励,于是我决定我要将JavaScript进行 ...

  8. UVA1376.Animal Run (最小割转为最短路 && dijkstra)

    Animal Run Time Limit:6000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu Submit Status ...

  9. 【PHP面向对象(OOP)编程入门教程】6.如何去使用对象中的成员

    上面看到PHP对象中的成员有两种一种是成员属性, 一种是成员方法.对象我们以经可以声明了,$p1=new Person(); 怎么去使用对象的成员呢?要想访问对象中的成员就要使用一个特殊的操作符”-& ...

  10. JAVA-多屏幕显示

    以下代码适用于:一台主机连接多台显示器,JAVA Swing窗口需要分别显示到对应的显示器上. GraphicsEnvironment env = GraphicsEnvironment.getLoc ...