找到数组或整数列表中连续子序列的最大和

题目描述:

The maximum sum subarray problem consists in finding the maximum sum of a contiguous subsequence in an array or list of integers:

maxSequence([-2, 1, -3, 4, -1, 2, 1, -5, 4])
# should be 6: [4, -1, 2, 1]

Easy case is when the list is made up of only positive numbers and the maximum sum is the sum of the whole array. If the list is made up of only negative numbers, return 0 instead.

Empty list is considered to have zero greatest sum. Note that the empty list or array is also a valid sublist/subarray.

我的解答:

def maximum_subarray_sum(n):
if len(n) == 0:
return 0
max_sum = 0
max_tmp = 0
for i in range(len(n)):
for j in range(i + 1, len(n)):
pass
if max_tmp <= 0:
max_tmp = n[i]
else:
max_tmp += n[i]

if max_sum < max_tmp:
max_sum = max_tmp
return max_sum
原文地址:https://www.cnblogs.com/wlj-axia/p/12704997.html