我正在解决一个要求最大和连续子数组的问题。我知道这个问题可以通过使用 Kadane 的算法来解决,但我不知道算法,所以我尝试自己编写问题。
这是我写的代码:
def maxSubarray(arr): # 'arr' is the list for which we have to calculate the maximum sum
h1 = arr[0]
h2 = arr[1]
subset_sum = -sys.maxsize-1
for i in range(1,len(arr)):
h1 = max(h2,h1+arr[i])
subset_sum = max(h1,subset_sum)
subset_sum = max(max(arr),subset_sum) # This is used if all elements in the list are negative
但是这段代码没有给出正确的输出,我似乎无法弄清楚出了什么问题?谁能帮我吗?