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),因为它的实现并不是基于 ... 
随机推荐
- docker pull centos慢问题的解决方案
			1.现象 如果直接docker pull centos 两个小时才down下来8M,很慢 2.解决 [root@localhost network-scripts]# cd /etc/docker [ ... 
- centos7+hadoop完全分布式集群搭建
			Hadoop集群部署,就是以Cluster mode方式进行部署.本文是基于JDK1.7.0_79,hadoop2.7.5. 1.Hadoop的节点构成如下: HDFS daemon: NameN ... 
- leetcode1012
			# given number n, see whether n has repeated number def has_repeated(n): str_n = str(n) return len(s ... 
- leetcode1003
			class Solution: def isValid(self, S: str) -> bool: n = len(S) if n % 3 != 0: return False while n ... 
- spark sql加载avro
			1.spark sql可以直接加载avro文件,之后再进行一系列的操作,示例: SparkConf sparkConf = new SparkConf().setAppName("Spark ... 
- 优秀的 Go 存储开源项目和库
			可以看到,今年谷歌家的 Go 编程语言流行度有着惊人的上升趋势,其发展也是越来越好,因此本文整理了一些优秀的 Go 存储相关开源项目和库,一起分享,一起学习. 存储服务器(Storage Server ... 
- 3:while、for 循环语句
			循环就是重复的做一件事情.python 中的循环语句有 while 和 for. while 循环 while 循环必须得有一个计数器,否则会变成一个死循环. # 例如这段代码,这段程序运行之后会一直 ... 
- oracle vm突然黑屏了
			装完mongodb-compass后,我重启了下虚拟机,发现突然进不到ubuntu系统了,一开起来就黑屏.网上查了下有的说是显卡问题的,有说是内核问题的,但我啥都没干突然间就黑屏了.折腾了一下午没搞定 ... 
- ImportError: No module named etree.ElementTree问题解决方法
			学习python操作xml文档过程中碰到的ImportError: No module named etree.ElementTree问题,问题现象比较奇怪,做个记录. 操作环境 Python3.6+ ... 
- How ASP.NET MVC Works ? (Artech)
			一.ASP.NET + MVC IIS与ASP.NET管道 MVC.MVP以及Model2[上篇] MVC.MVP以及Model2[下篇] ASP.NET MVC是如何运行的[1]: 建立在“伪”MV ... 
