问题描述:
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.
中文描述:
-------------------------------最大间隙问题--------------------------------
问题描述:最大间隙问题:给定n个实数x1,x2,...,xn,求这n个数在实轴上相邻2个数之间的最大差值。
假设对任何实数的下取整函数耗时O(1),设计解最大间隙问题的线性时间算法。
* 算法设计:对于给定的n个实数x1,x2,...,xn,计算它们的最大间隙。
* 数据输入:输入数据由文件名为input.txt的文本文件提供。文件的第一行有1个正整数n。接下来的一
行中有n个实数x1,x2,...,xn。
* 结果输出:将找到的最大间隙输出到文件output.txt。
输入文件示例 输出文件示例
input.txt output.txt
5 3.2
2.3 3.1 7.5 1.5 6.3
算法思想:
这道题最直接的解法是,先排序,得到有序数组,然后再对相邻元素作差,找出差最大的。一般排序算法的最有复杂度为O(nlogn),不满足题意。
故采取桶排序的方法:
1. 找到n个数据中最大和最小数据maxx和minx;
2. 用n-2个点等分区间[minx,maxx],即将[minx,maxx]等分为n-1个区间(前闭后开区间),将这些区间看做桶,编号为1,2,...,n-2,n-1,且桶i的上界和桶i+1的下届相同,即每个桶的大小相同;
每个桶的大小为:avergap=(maxx-minx)/(n-1)
编程实现中,用以下数据结果存放有关桶的数据:
int *count=new int[n]; //实际分到每个桶的数据个数
double *low=new double[n]; //实际分到每个桶的最小数据
double *high=new double[n]; //实际分到每个桶的最大数据
3. 将n个数放入n-1个桶中:
3.1 按如下规则将x[i]分配到某个桶(编号index)中: index=int((x[i]-minx)/avergap)+1;
若x[i]=minx,则被分到第1个桶中(minx即为桶1的下界);
若x[i]=桶j的下界(也是桶j-1的上界),则被分到桶j中(j>=1);
若x[i]=maxx,则被分到桶n中(max为桶n的下界桶n-1的上界),但没有桶n,解决办法:
可人为将其移入桶n-1中或者再加一个桶,这并不影响求其最大间隙;
3.2 调整分到该桶的最大最小数据;
4. 求最大间隙:
除最大最小数据maxx和minx以外的n-2个数据被放入n-1个桶中,由鸽巢原理可知至少有一个桶是空的;
又因每个桶的大小相同,所以最大间隙不会在同一桶中出现;
一定是某个桶的上界(High)和其后某个桶的下界(Low)之间隙,且该两桶之间的桶(即编号在该两桶编号之间的桶)一定是空桶;
即最大间隙在桶i的上界和桶j的下界之间产生(j>=i+1);
#####################################################################################
源码运行平台:Visual Stdio 2010 注意需要在工程文件夹内创建input.txt和output.txt
/*-------------------------------最大间隙问题--------------------------------*/
/* 问题描述:最大间隙问题:给定n个实数x1,x2,...,xn,求这n个数在实轴上相邻2个数之间的最大差值。
假设对任何实数的下取整函数耗时O(1),设计解最大间隙问题的线性时间算法。
* 算法设计:对于给定的n个实数x1,x2,...,xn,计算它们的最大间隙。
* 数据输入:输入数据由文件名为input.txt的文本文件提供。文件的第一行有1个正整数n。接下来的一
行中有n个实数x1,x2,...,xn。
* 结果输出:将找到的最大间隙输出到文件output.txt。
*/
/* 输入文件示例 输出文件示例
input.txt output.txt
5 3.2
2.3 3.1 7.5 1.5 6.3
*/
/*
@author caigen001
@time 2019/3/22
*/
#include <iostream>
#include <fstream>
using namespace std;
int maxi(int n, double x[])//get the index of the max number
{
int m = 1;
for(int i = 1; i < n+1; i++)
{
if(x[i] > x[m])
m = i;
}
return m;
}
int mini(int n, double x[]) //get the index of the min number
{
int m = 1;
for(int i = 1; i < n+1; i++)
{
if(x[i] < x[m])
m = i;
}
return m;
}
double MaxGap(int n, double x[])
{
double maxx = x[maxi(n, x)];//the max number
double minx = x[mini(n, x)];//the min number
int *count = new int[n+1];//save the sum of the numbers
double *high = new double[n+1];//save the max of the numbers in every bucket
double *low = new double[n+1];//save the min of the numbers in every bucket
for(int i = 1; i < n+1; i++) //initialization
{
count[i] = 0;
high[i] = minx;
low[i] = maxx;
}
for(int i = 1; i < n+1; i++) //label the bucket number
{
//minx | |...| | maxx,split by |(n-2),the sum of bucket is n-1;
double avergap = (maxx - minx)/(n-1); //the length of the bucket gap
int index = int((x[i] - minx)/avergap)+1;//bucket number:1, 2...n-1
if(index == n)//put the max number in the last bucket
index = n-1;
count[index]++;
if(x[i] > high[index]) //get the max of every bucket
high[index] = x[i];
if(x[i] < low[index]) //get the min of every bucket
low[index] = x[i];
}
double temp = 0, left = high[1];
for(int i = 1; i < n; i++) //find the max gap of numbers
{
if(count[i])//this bucket is not empty
{
double thisgap = low[i] - left;
if(thisgap > temp) //get the max bucket gap
{
temp = thisgap;
}
left = high[i];
}
}
return temp;
}
void main()
{
ifstream inputfile("input.txt");
ofstream outputfile("output.txt");
int n = -1;
inputfile >> n;
double *x = new double[n+1];
for(int i = 1; i < n+1; i++)
{
inputfile >> x[i];
}
outputfile << "The maxgap is :" << MaxGap(n, x);
cout << MaxGap (n, x) <<endl;
system("pause");
}
参考资料: