我正在使用以下程序来查找总和的最大总和和索引。我能够获得正确的索引,但无法找到正确的索引。
def max_sum_new(a):
max_current = max_global = a[0]
r_index = 0
for i in range(1, len(a)):
max_current = max(a[i], max_current+a[i])
if max_current > max_global:
max_global = max_current
r_index = i
return (max_global, r_index)
#Driver Code:
my_arr = [-2, 3, 2, -1]
print(max_sum_new(my_arr))
我正进入(状态
(5, 2)
这是预期的,但我也想获得在这种情况下应该是 1 的起始索引
所以我期待
(5, 1, 2)
有什么方法可以在这里获取起始索引吗?我应该如何把变量记录开始索引?