QUESTION

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.

1ST TRY

桶排序

class Solution {
public:
int maximumGap(vector<int> &num) {
if(num.empty() || num.size() < )
return ;
int maxNum = *max_element(num.begin(), num.end());
int minNum = *min_element(num.begin(), num.end()); //bucket gap: 假设每个数一个桶,两个桶之间的平均差值
int gap = ceil((double)(maxNum - minNum)/(num.size()-));
//number of buckets
int bucketNum = (maxNum-minNum)/gap+;
//declare buckets
vector<int> bucketsMin(bucketNum, INT_MAX);
vector<int> bucketsMax(bucketNum, INT_MIN);
//put into buckets
for(int i = ; i < num.size(); i ++)
{
int buckInd = (num[i]-minNum)/gap; //匹配到bucket
bucketsMin[buckInd] = min(bucketsMin[buckInd], num[i]);
bucketsMax[buckInd] = max(bucketsMax[buckInd], num[i]);
} //i_th gap is minvalue in i+1_th bucket minus maxvalue in i_th bucket
int maxGap = INT_MIN;
int previous = minNum;
for(int i = ; i < bucketNum; i ++)
{
if(bucketsMin[i] == INT_MAX && bucketsMax[i] == INT_MIN)
continue; //empty
maxGap = max(maxGap, bucketsMin[i]-previous);
previous = bucketsMax[i];
}
return maxGap;
}
};

Result: Accepted

Maximum Gap (ARRAY - SORT)的更多相关文章

  1. 164. Maximum Gap (Array; sort)

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

  2. 【leetcode】Maximum Gap

    Maximum Gap Given an unsorted array, find the maximum difference between the successive elements in ...

  3. [LintCode] Maximum Gap 求最大间距

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

  4. leetcode[164] Maximum Gap

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

  5. 【leetcode 桶排序】Maximum Gap

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

  6. 【LeetCode】164. Maximum Gap (2 solutions)

    Maximum Gap Given an unsorted array, find the maximum difference between the successive elements in ...

  7. LeetCode 164. Maximum Gap[翻译]

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

  8. 【刷题-LeetCode】164 Maximum Gap

    Maximum Gap Given an unsorted array, find the maximum difference between the successive elements in ...

  9. 线性时间的排序算法--桶排序(以leetcode164. Maximum Gap为例讲解)

    前言 在比较排序的算法中,快速排序的性能最佳,时间复杂度是O(N*logN).因此,在使用比较排序时,时间复杂度的下限就是O(N*logN).而桶排序的时间复杂度是O(N+C),因为它的实现并不是基于 ...

随机推荐

  1. 02.centos6.4找不到ifcfg-eth0(静态ip配置)

    1.默认情况在/etc/sysconfig/network-scripts/目录下面找不到ifcfg-eth0文件,我们需要手动copy 1.1动态ip配置 #cp ifcfg-lo ifcfg-et ...

  2. 深度学习原理与框架-神经网络-cifar10分类(代码) 1.np.concatenate(进行数据串接) 2.np.hstack(将数据横着排列) 3.hasattr(判断.py文件的函数是否存在) 4.reshape(维度重构) 5.tanspose(维度位置变化) 6.pickle.load(f文件读入) 7.np.argmax(获得最大值索引) 8.np.maximum(阈值比较)

    横1. np.concatenate(list, axis=0) 将数据进行串接,这里主要是可以将列表进行x轴获得y轴的串接 参数说明:list表示需要串接的列表,axis=0,表示从上到下进行串接 ...

  3. windows7 安装虚拟机,xsheel连接不上的问题,记录一下

    安装了好久,一直连接不上...,原来是网络没开..重新安装设置就可以了!!!记录一下

  4. Zabbix笔记

    简单检查中的icmppingloss[<target>,<packets>,<interval>,<size>,<timeout>]   结 ...

  5. b2BuoyancyController 使用浮力

    package{ import Box2D.Collision.b2AABB; import Box2D.Collision.b2RayCastInput; import Box2D.Collisio ...

  6. 【359】scikit learn 官方帮助文档

    官方网站链接 sklearn.neighbors.KNeighborsClassifier sklearn.tree.DecisionTreeClassifier sklearn.naive_baye ...

  7. npm run dev 报错:missing script:dev

    一.问题: 今天在运行vue项目时,在mac终端输入npm run dev,结果报错: 翻译是: npm错误:缺少script:dev npm错误:完整路径见:users/mymac/ .npm/_l ...

  8. 【原创】锐捷实现OSPF路由协议和NAT地址转换协议

    路由网络设计与实施 [锐捷设备实现OSPF路由协议与NAT地址转换] 说明:   本文是在多VLAN双星型交换网络的基础之上发展的.关于组建多VLAN双星型交换网络,请参阅: <思科和锐捷组建多 ...

  9. jndi 小案例

    JNDI就是为JAVA中命名和目录服务定义的JAVA API,是命名服务的抽象机制.在J2EE中,JNDI的目的是用来查找J2EE服务器的注册资源.只要该对象在命名服务器上注册过,且你知道命名服务器的 ...

  10. Hibernate 再接触 CRUD

    1.save 一对多双向 package com.bjsxt.hibernate; import java.util.HashSet; import java.util.Set; import jav ...