Maximum Gap 164

题目描述:

给出一个没有排序的数组,找出这个数组中数字排序之后相邻元素的最大差值

给出的数字都是整数,且范围在32位整数范围内

要求时间复杂,空间复杂度都是线性复杂度


题目分析:

最简单的方法就是排序之后,找相邻元素之间的最大差值

但是时间复杂度为 O(nlogn)

这个题考察的是排序之后的情况,那么看来还是要适当的排序,考虑到要求复杂度线性,可以用桶排序

将数组中的n个元素,分到n个桶中

桶的宽度定义为 d=(max-min)/n,d也是元素之间的平均差值,

这样一定有排序之后相邻元素之间的差值大于等于d(d是平均差值,总要有个大于平均数的吧,要不就全部相同),所以我们可以不考虑桶内元素之间的差值(因为差值一定小于d)

因为不需要考虑桶内元素之间的差值,每个桶可以只记录一个最大值和一个最小值,每个桶的最大值和下一个有数据的桶的最小值一定是排序之后相邻的

只需要找出每个桶中最大值和下一个有数据的桶中的最小值之间的差值,并取一个最大的差值,就是我们要的结果了


代码:

 1 int maximumGap(vector<int> &num) {
 2         int len=num.size();
 3         if(len<2)return 0;
 4         int bucketL[len+1];
 5         int bucketR[len+1];
 6         memset(bucketL,-1,sizeof(bucketL));
 7         memset(bucketR,-1,sizeof(bucketR));
 8
 9         int ma=0;
10         int mi=(1<<31)-1;
11         for(int i=0;i<len;i++){
12                 if(ma<num[i])ma=num[i];
13                 if(mi>num[i])mi=num[i];
14         }
15         double  gap=double(ma-mi+1)/len;  ///计算gap时要加1是为了让最大的数字落在第len-1个桶中,而不是第len个桶中
16         for(int i=0;i<len;i++){
17                 int bi=(int)((num[i]-mi)/gap);
18
19                 if(bucketL[bi]==-1)bucketL[bi]=num[i];
20                 else if(bucketL[bi]>num[i])bucketL[bi]=num[i];
21                 if(bucketR[bi]==-1)bucketR[bi]=num[i];
22                 else if(bucketR[bi]<num[i])bucketR[bi]=num[i];
23         }
24
25         int ans=0;
26         int pre=-1;
27         for(int i=0;i<len;i++){
28                 if(bucketL[i]!=-1){
29                         if(pre!=-1) ans=max(bucketL[i]-pre,ans);
30                         pre=bucketL[i];
31                 }
32                 if(bucketR[i]!=-1){
33                         pre=bucketR[i];
34                 }
35         }
36         return ans;
37 }
时间: 2024-11-22 17:13:59

Maximum Gap 164的相关文章

164. Maximum Gap

/* * 164. Maximum Gap * 2016-6-4 by Mingyang * 这个题目首先要求的是linear的时间,所以我个人的预测就是bucketsort * bucketsort就是把一个list分成几个bucket再分别把每一个桶排序,再合起来 * 比如我现在有10个如果selection sort就是100的时间复杂度,那么需要分成两个5 * 就是两个25相加,就是50 * 假设有N个元素A到B. * 那么最大差值不会小于ceiling[(B - A) / (N - 1

No.164 Maximum Gap

No.164 Maximum Gap 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

LeetCode – Refresh – Maximum Gap

Sorting solution O(nlogn): 1 class Solution { 2 public: 3 int maximumGap(vector<int> &num) { 4 int len = num.size(), result = 0; 5 if (len < 2) return 0; 6 sort(num.begin(), num.end()); 7 for (int i = 0; i < len-1; i++){ 8 result = max(res

[LintCode] Maximum Gap 求最大间距

Given an unsorted array, find the maximum difference between the successive elements in its sorted form. Return 0 if the array contains less than 2 elements. Notice You may assume all elements in the array are non-negative integers and fit in the 32-

leetcode 155: Maximum Gap

Maximum Gap Total Accepted: 2946 Total Submissions: 12695 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 elemen

【leetcode 桶排序】Maximum Gap

1.题目 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-

[leedcode 164] Maximum Gap

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-negat

LeetCode:164. Maximum Gap

这道题比较简单,虽然不知道为什么被贴上了困难的标签~ 贴上题目: Given an unsorted array, find the maximum difference between the successive elements in its sorted form. Return 0 if the array contains less than 2 elements. 中文翻译: 现在有一个无序数组,找出数组在排序后,相邻元素差值的最大值 如果元素个数少于两个,就返回0. emmmmm

164. Maximum Gap(js)

给定一个无序的数组,找出数组在排序之后,相邻元素之间最大的差值. 如果数组元素个数小于 2,则返回 0. 示例 1: 输入: [3,6,9,1]输出: 3解释: 排序后的数组是 [1,3,6,9], 其中相邻元素 (3,6) 和 (6,9) 之间都存在最大差值 3.示例 2: 输入: [10]输出: 0解释: 数组元素个数小于 2,因此返回 0.说明: 你可以假设数组中所有元素都是非负整数,且数值在 32 位有符号整数范围内.请尝试在线性时间复杂度和空间复杂度的条件下解决此问题. Given a