Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/bash/15.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 这个求最大绝对差的解与我的解有什么关系?_Python_Python 3.x - Fatal编程技术网

Python 这个求最大绝对差的解与我的解有什么关系?

Python 这个求最大绝对差的解与我的解有什么关系?,python,python-3.x,Python,Python 3.x,我想了解用于查找最大绝对差的优化代码如何与最大差的计算方法相关 这就是挑战 给你一个由N个整数组成的数组,A1,A2,…,an。返回最大值 所有1的f(i,j)值≤ i、 j≤ N.f(i,j)定义为| A[i]- A[j]|+| i-j |,其中| x |表示x的绝对值 这就是我的方法: def maxArr(self, A): if len(A) == 0: return 0 curr_diff = 0 max_diff = 0 for i i

我想了解用于查找最大绝对差的优化代码如何与最大差的计算方法相关

这就是挑战

给你一个由N个整数组成的数组,A1,A2,…,an。返回最大值 所有1的f(i,j)值≤ i、 j≤ N.f(i,j)定义为| A[i]- A[j]|+| i-j |,其中| x |表示x的绝对值

这就是我的方法:

def maxArr(self, A):
    if len(A) == 0:
        return 0
    curr_diff = 0
    max_diff = 0
    for i in range(len(A)):
        j = i
        while j < len(A):
            curr_diff = abs(A[i] - A[j]) + abs(i - j)
            max_diff = max(max_diff, curr_diff)
            j += 1
    return max_diff

它通过了,我决定一步一步地阅读每一个代码来理解。我不确定是否有正确的逐步输出,但我反复检查,仍然得到以下值:

Using input of A = [1, 3, -1]

L is for the loop, the number next to L is the index from [1, 3, -1] --> (0, 1, 2). 

LOOP: 

L0 -  
max1 -> 1
min1 -> +1000000000
max2 -> 1
min2 -> +1000000000

L1 - 
max1 -> 4
min1 -> +1000000000
max2 -> 2
min2 -> +1000000000

L2 - 
max1 -> 4
min1 -> +1000000000
max2 -> 2
min2 -> +1000000000

RETURN: 
max((4 - (+1000000000)), (2 - (+1000000000))) = -999999996


我又看了一遍,得到了同样的号码。当我一步一步地编写代码时,得到了5,这是[1,3-1]的返回值和输出。但我只是想了解更好的解决方案是如何工作的


感谢您的帮助。提前感谢。

在L0中,
min1->+100000000
是错误的-什么是
min(100000000,1+0)
?哦,伙计。谢谢你,哈哈。
Using input of A = [1, 3, -1]

L is for the loop, the number next to L is the index from [1, 3, -1] --> (0, 1, 2). 

LOOP: 

L0 -  
max1 -> 1
min1 -> +1000000000
max2 -> 1
min2 -> +1000000000

L1 - 
max1 -> 4
min1 -> +1000000000
max2 -> 2
min2 -> +1000000000

L2 - 
max1 -> 4
min1 -> +1000000000
max2 -> 2
min2 -> +1000000000

RETURN: 
max((4 - (+1000000000)), (2 - (+1000000000))) = -999999996