Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/321.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/mercurial/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 在差异至少为x的数组中查找最接近的两个值_Python_Arrays_Sliding Window - Fatal编程技术网

Python 在差异至少为x的数组中查找最接近的两个值

Python 在差异至少为x的数组中查找最接近的两个值,python,arrays,sliding-window,Python,Arrays,Sliding Window,任务:给定一个未排序的数组,如何找到最接近的两个值,使其绝对差值至少为x(如果数组中没有两个元素具有此差值,则返回-1) 我的方法:我尝试使用滑动窗口方法: 我从索引0开始第一个指针,从索引1开始第二个指针 我递增索引1,直到得到第一个和第二个索引处的数组元素之间的差值至少为x 在那之后,我开始增加第一个索引直到第二个索引,跟踪中间的任何值,这些值小到足以匹配至少x的差值 我这样做直到第二个指针指向数组的末尾 下面是我的代码和一个示例: length = 6 x = 5 arr = [1,

任务:给定一个未排序的数组,如何找到最接近的两个值,使其绝对差值至少为x(如果数组中没有两个元素具有此差值,则返回-1)

我的方法:我尝试使用滑动窗口方法:

  • 我从索引0开始第一个指针,从索引1开始第二个指针
  • 我递增索引1,直到得到第一个和第二个索引处的数组元素之间的差值至少为x
  • 在那之后,我开始增加第一个索引直到第二个索引,跟踪中间的任何值,这些值小到足以匹配至少x的差值
  • 我这样做直到第二个指针指向数组的末尾
下面是我的代码和一个示例:

length = 6
x = 5
arr = [1, 1, 4, 3, 7, 5]

smallestRange = float('inf')
first = 0
second = 1

while (second < length):
    
    # Keep going until the proper difference is found
    while (second < length and abs(arr[first] - arr[second]) < x):
        second += 1
    
    # No matches are found
    if (second == length):
        break

    # Let first catch up to second
    minVal = first
    while (first != second):
        if (arr[first] <= arr[minVal] and abs(arr[first] - arr[second]) >= x):
            minVal = first
        first += 1
    
    if (minVal != second):
        if (second - minVal < smallestRange):
            smallestRange = second - minVal
    
    second += 1

if (smallestRange == float('inf')):
    smallestRange = -1
print(smallestRange)
length=6
x=5
arr=[1,1,4,3,7,5]
smallestRange=float('inf')
第一个=0
秒=1
而(秒<长度):
#继续,直到找到正确的差异
而(秒<长度和abs(arr[first]-arr[second])
问题:这并没有涵盖所有可能的情况。例如,如果第一个元素与其他元素没有任何绝对差异,那么我的第二个指针将直接指向末尾,我的代码只返回-1。我也尝试过手动检查其他输入数组,但有时不起作用。我能得到一些关于如何解决这个问题的建议吗


该示例应打印3,因为1和7是最接近的一对,其差值至少为5。

是否需要手动执行此操作?是什么阻止你在任何算法之前执行排序?我一直在手工测试它,看看它是否适用于多个输入。我不能排序的主要原因是我试图在O(n)中进行排序,我很有信心这不能在O(n)中进行。自然搜索空间是O(n²),你只能做这么多来利用问题的对称性。观察每个元素,一个恒定的时间数是不够的,因为您总是可以构造一个反例,其中两个数字之间的距离大于这个恒定的数字。