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. HTML5 Canvas 小例子 简易画板

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...

  2. leetcode991

    class Solution: def brokenCalc(self, X: 'int', Y: 'int') -> 'int': if X>=Y : return Y-X else: ...

  3. configparser 模块

    import configparser #按字典方式操作 config = configparser.ConfigParser() #创建config 对象 #config对象test 等于 {'na ...

  4. oracle 内连接(inner join)、外连接(outer join)、全连接(full join)

    转自:https://premier9527.iteye.com/blog/1659689 建表语句: create table EMPLOYEE(EID NUMBER,DEPTID NUMBER,E ...

  5. preparedstatement 为什么可以防止sql注入

    有大神总结的很好,,参考文献 http://www.importnew.com/5006.html preparedstatement优势:sql的预编译(数据库层面完成)提升效率. 为什么可以防止s ...

  6. 今天学习到的几条shell技巧

    1.获取某个进程的进程号 PID=`ps aux | grep 进程名 | grep -v "grep" | awk '{print $2}'` 2.获取某个进程的CPU(同理可获 ...

  7. python实现Excel删除特定行、拷贝指定行操作

    工作中遇到的,本来用VBA写的,操作很慢,尝试用Python实现, 任务需求: 从原始的两张表中拷贝行到五张表中,如下表所示: source1和source2是一样的格式:         one t ...

  8. C#调用C++

    c++ extern "C" __declspec(dllexport) char* WINAPI base64_decode( char *data,char base[]) { ...

  9. Delphi 不使用自带模板创建服务

    program Project1; uses Windows, WinSvc; const ServiceName: pchar = 'SnowWings Service'; DisplayName: ...

  10. asp.net 如何判断输入的值 包括 汉字?

    string input = " 里面是不是汉字 ";bool bl= System.Text.RegularExpressions.Regex.IsMatch(input, @& ...