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.

class Solution {
public:
int maximumGap(vector<int>& nums) {
int n = nums.size(), mini, maxi, i, j, k, prev = -, ans = ;
if(n < )
return ;
mini = maxi = nums[];
for(i = ; i < n; i++)
{
if(nums[i] < mini)
mini = nums[i];
if(nums[i] > maxi)
maxi = nums[i];
}
k = (maxi - mini) / n + ;
vector<vector<int>> v(n);
for(i = ; i < n; i++)
{
j = (nums[i] - mini) / k;
if( == v[j].size())
{
v[j].push_back(nums[i]);
v[j].push_back(nums[i]);
}
else
{
v[j][] = max(nums[i], v[j][]);
v[j][] = min(nums[i], v[j][]);
}
}
for(i = ; i < n; i++)
{
if( == v[i].size())
continue;
if(prev != -)
{
j = v[i][] - v[prev][];
ans = max(ans, j);
}
prev = i;
}
return ans;
}
};

将nums分为n组,每组k = (maxi - mini) / n + 1个数,分别求出每组的最大值和最小值。

164. Maximum Gap *HARD* -- 无序数组找出排序后连续元素的最大间隔的更多相关文章

  1. leetcode[164] Maximum Gap

    梅西刚梅开二度,我也记一题. 在一个没排序的数组里,找出排序后的相邻数字的最大差值. 要求用线性时间和空间. 如果用nlgn的话,直接排序然后判断就可以了.so easy class Solution ...

  2. LeetCode 164. Maximum Gap[翻译]

    164. Maximum Gap 164. 最大间隔 Given an unsorted array, find the maximum difference between the successi ...

  3. ✡ leetcode 164. Maximum Gap 寻找最大相邻数字差 --------- java

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

  4. 164. Maximum Gap

    题目: Given an unsorted array, find the maximum difference between the successive elements in its sort ...

  5. 【Java】 剑指offer(2) 不修改数组找出重复的数字

    本文参考自<剑指offer>一书,代码采用Java语言. 更多:<剑指Offer>Java实现合集 题目 在一个长度为n+1的数组里的所有数字都在1到n的范围内,所以数组中至少 ...

  6. 《剑指offer》第三_二题(不修改数组找出重复的数字)

    // 面试题3(二):不修改数组找出重复的数字 // 题目:在一个长度为n+1的数组里的所有数字都在1到n的范围内,所以数组中至 // 少有一个数字是重复的.请找出数组中任意一个重复的数字,但不能修改 ...

  7. C#比较二个数组并找出相同或不同元素的方法

    这篇文章主要介绍了C#比较二个数组并找出相同或不同元素的方法,涉及C#针对数组的交集.补集等集合操作相关技巧,非常简单实用, 具有一定参考借鉴价值,需要的朋友可以参考下 " }; " ...

  8. 一起来刷《剑指Offer》——不修改数组找出重复的数字(思路及Python实现)

    数组中重复的数字 在上一篇博客中<剑指Offer>-- 题目一:找出数组中重复的数字(Python多种方法实现)中,其实能发现这类题目的关键就是一边遍历数组一边查满足条件的元素. 然后我们 ...

  9. 如何寻找无序数组中的第K大元素?

    如何寻找无序数组中的第K大元素? 有这样一个算法题:有一个无序数组,要求找出数组中的第K大元素.比如给定的无序数组如下所示: 如果k=6,也就是要寻找第6大的元素,很显然,数组中第一大元素是24,第二 ...

随机推荐

  1. Margin and Padding in Windows Forms Controls

    https://msdn.microsoft.com/en-us/library/ms229627.aspx Margin and Padding Precise placement of contr ...

  2. Horizontal Toolbar With Navigational Buttons Form Sample For Oracle Forms 10g/11g

    Sharing an Oracle Form Htoolbar.fmb for Oracle Forms 10g/11g containing Horizontal Toolbar canvas an ...

  3. python_way ,day7 面向对象 (初级篇)

    面向对象 初级篇   python支持 函数 与 面向对象   什么时候实用面向对象? 面向对象与函数对比 类和对象 创建类 class 类名 def 方法名(self,xxxx) 类里面的方法,只能 ...

  4. STORM_0010_Message passing implementation/消息传递的实现

    下面是0.8.0之前的表述,之后的已经基于Disruptor改造过了 这个文章演示了发射和转移tuple是怎么在storm中工作的   Worker为消息传递负责 当zk中的任务出现了变化或者每个ta ...

  5. 本地设置正常,放服务器上就报 System.Security系统找不到指定的文件解决方法

    在应用程序池设置中将“加载用户配置文件”(Load User Profile)设置为true,问题就解决.

  6. HIHO 线段树(单点)

    #include <stdio.h> #include <string.h> #include <math.h> #include <iostream> ...

  7. [转载] C++ 多线程编程总结

    原文: http://www.cnblogs.com/zhiranok/archive/2012/05/13/cpp_multi_thread.html 在开发C++程序时,一般在吞吐量.并发.实时性 ...

  8. ubuntu源码安装django

    由于用pip install django方法安装太慢,而且容易报错,故使用源码的方式安装 方法: 下载源码包:https://www.djangoproject.com/download/ 输入以下 ...

  9. Round robin

    http://www.computerhope.com/jargon/r/rounrobi.htm Round robin Round robin is a method of distributin ...

  10. CSS3_边框属性之圆角

    一.border-radius是向元素添加圆角边框: border-radius的值不仅能用px单位,你还可以用百分比或者em,但兼容性目前还不太好.(都不能是负值) 1.border-radius ...