Maximum Gap (ARRAY - SORT)
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)的更多相关文章
- 164. Maximum Gap (Array; sort)
Given an unsorted array, find the maximum difference between the successive elements in its sorted f ...
- 【leetcode】Maximum Gap
Maximum Gap Given an unsorted array, find the maximum difference between the successive elements in ...
- [LintCode] Maximum Gap 求最大间距
Given an unsorted array, find the maximum difference between the successive elements in its sorted f ...
- leetcode[164] Maximum Gap
梅西刚梅开二度,我也记一题. 在一个没排序的数组里,找出排序后的相邻数字的最大差值. 要求用线性时间和空间. 如果用nlgn的话,直接排序然后判断就可以了.so easy class Solution ...
- 【leetcode 桶排序】Maximum Gap
1.题目 Given an unsorted array, find the maximum difference between the successive elements in its sor ...
- 【LeetCode】164. Maximum Gap (2 solutions)
Maximum Gap Given an unsorted array, find the maximum difference between the successive elements in ...
- LeetCode 164. Maximum Gap[翻译]
164. Maximum Gap 164. 最大间隔 Given an unsorted array, find the maximum difference between the successi ...
- 【刷题-LeetCode】164 Maximum Gap
Maximum Gap Given an unsorted array, find the maximum difference between the successive elements in ...
- 线性时间的排序算法--桶排序(以leetcode164. Maximum Gap为例讲解)
前言 在比较排序的算法中,快速排序的性能最佳,时间复杂度是O(N*logN).因此,在使用比较排序时,时间复杂度的下限就是O(N*logN).而桶排序的时间复杂度是O(N+C),因为它的实现并不是基于 ...
随机推荐
- MemberShip的 链接字符串的使用
1.运行asp.net Sql Server注册工具:aspnet_regsql.exe,详细参见:http://msdn.microsoft.com/zh-cn/library/ms229862(v ...
- 【Source Insight 】之marco学习笔记1
我们学习编程语言都是从Hello World!,现在我们学习marco也不例外 打开C:\Users\%USERPROFILE%\Documents\Source Insight 4.0\Projec ...
- 机器学习入门-文本特征-word2vec词向量模型 1.word2vec(进行word2vec映射编码)2.model.wv['sky']输出这个词的向量映射 3.model.wv.index2vec(输出经过映射的词名称)
函数说明: 1. from gensim.model import word2vec 构建模型 word2vec(corpus_token, size=feature_size, min_count ...
- 02-body标签中相关标签-1
主要内容: 字体标签: h1~h6.<font>.<u>.<b>.<strong><em>.<sup>.<sub> ...
- XML中的变量传值
在action的java类中定义变量之后,在XML中获取该变量进行对应传值:: 在指定方法中获取XML配置文件的变量传值::
- printf 输出% 和 \
在小白第一章后面1.5.3中有仨题: 1 试着把%d中的两个字符(百分号和小写字母d)输出到屏幕. 2 试着把\n中的两个字符(反斜线和小写字母n)输出到屏幕. 3 像 1.2这样需要“特殊方法”才能 ...
- oracle第三天笔记
DDL语句管理表 /* Oracle体系结构: 数据库 ---> 数据库实例ORCL ---> 表空间 (用户里面的创建表) ---> 数据文件 地球 ---> 中国 ---& ...
- 12.常用类简单介绍.md
目录 1.Scanner类 2.System类 4.Object类和工具类 5.StringBuffer类和StringBuilder类 6.Math类 7.Random类和ThreadLocalRa ...
- Hibernate学习笔记3.3(Hibernate组建映射2)
多对多 相当于一个老师可以教多个学生,一个学生也可以有多个老师 数据表中都是再设计一个表寸相关的id 1.多对多单向 1annotation Student.java package com.bjsx ...
- 3D模板阴影原理
3D模板阴影原理 1:先从3dsMax中导出一个简单的场景,一个园环,球,平面. 2:园环直接面向光源,园环对球体来说是一个光线的阻挡物,园环在它上面形成阴影,同时,园环和球体对平面来说是光线的阻挡物 ...