在O(n)的时间复杂度的算法来找到一对NOS的一个数组,相互之间最接近的区别复杂度、相互之间、数组、算法

2023-09-11 23:21:43 作者:临淵不羡鱼

我给出一个整数数组它们不一定是排序。我一定要找到一对一氧化氮合酶,其至少是比其他任何一对编号的阵列中彼此之间的差异。时间效率应该是O(N)。

i am given an array of integers which are not necessarily sorted. I have to find a pair of nos whose difference between each other is least compared to any of the other pair of nos in the array. the time efficiency should be O(n).

推荐答案

我是pretty的肯定,你不能让一个一般的线性时间算法对于这个问题!

I'm pretty sure you cannot get a general linear time algorithm for this problem!

不过,既然你有(有界)的整数,你可以骗一点,使用基数排序,这是线性时间排序数组开始!然后,只需找到最接近相邻的一对,而这又是线性的。

However, since you have (bounded) integers, you can cheat a little and start by sorting the array using radix sort, which is linear time! Then just find the closest adjacent pair, which is linear again.