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. 白鹭引擎 - 遮罩( Rectangle )

    1: 矩形遮罩 class Main extends egret.DisplayObjectContainer { /** * Main 类构造器, 初始化的时候自动执行, ( 子类的构造函数必须调用 ...

  2. HTML5 Canvas 小例子 伸缩旋转方块

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

  3. sql server 字符串字节长度

    SQL Server 字符个数,字节长度,len不是你想要的字节数,datalength才能得到字节数 select len('娜娜123') ,datalength('娜娜123') 5       ...

  4. 比较sql server两个数据库

    比较sql server两个数据库 http://opendbdiff.codeplex.com/ http://www.red-gate.com/ 有SQL Compare和SQL Prompt 开 ...

  5. nginx压缩,缓存

    https://www.darrenfang.com/2015/01/setting-up-http-cache-and-gzip-with-nginx/ https://www.linuxdashe ...

  6. leetcode AC1 感受

    在网上第一个AC还是蛮高兴的,之前试了不少练习编程的方法,感觉不怎么适合自己,在OJ上做题的确是一个能够锻炼的方法. 之前一直研究学习的方法,往简单的说是认知.练习,往复杂的说是,保持足够input, ...

  7. Linux命令:history

    显示历史(执行过的)命令. history [n] history -c history -d offset history -anrw [filename] history -p arg [arg ...

  8. UNITY2018开启deepprofiling

    ADB方式调试游戏步骤 前提: 1,手机开启 [开发者模式][USB调试] 2,数据线连接手机和电脑 3,安装adb(注意adb版本不对可能导致adb deveices找不到设备,那就换个adb版本) ...

  9. git 合并多个commit

    1,查看提交历史,git log 首先你要知道自己想合并的是哪几个提交,可以使用git log命令来查看提交历史,假如最近4条历史如下: commit 3ca6ec340edc66df13423f36 ...

  10. checkbox中jQuery对数组和对象的操作

    ------------------------------------------------------------------------------------------ 来段小例子,jQu ...