Find the contiguous subarray within an array (containing at least one number) which has the largest sum.
For example, given the array [−2,1,−3,4,−1,2,1,−5,4]
,
the contiguous subarray [4,−1,2,1]
has the largest sum = 6
.
求子区间最大值
前缀和加到负数就重新累加,因为前面的数加进来只会减小总和。
class Solution:
# @param A, a list of integers
# @return an integer
def maxSubArray(self, A):
maxSum, sum = A[0], A[0]
for i in xrange(1, len(A)):
if sum > 0:
sum += A[i]
else:
sum = A[i]
maxSum = max(maxSum, sum)
return maxSum