我给出一个整数数组它们不一定是排序。我一定要找到一对一氧化氮合酶,其至少是比其他任何一对编号的阵列中彼此之间的差异。时间效率应该是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.
上一篇:查找阵列组合组合、阵列