Brief analysis see: http://blog.csdn.net/shiquxinkong/article/details/17934747
Def FindGreatestSumOfSubArray (array, index = None): curSum = 0 maxSum = 0 # return maxSum without the start and end indexif index = None: for x in array: curSum = max (curSum, 0) curSum + = xmaxSum = max (maxSum, curSum) return maxSum # return maxSum and the start and end indexelse: curS =-1 curE =-1 maxS =-1 maxE =-1for I in range (len (array): if curSum <= 0: curSum = array [I] curS = icurE = ielse: curSum + = array [I] curE = iif curSum> maxSum: maxS = curSmaxE = curEmaxSum = curSumreturn maxSum, maxS, maxE
Here, we use the index parameter to overload two versions. One of them only returns the maximum child segment and the other, but sometimes we may be asked to return the start range and get the maximum child segment.
Although the Code above does not look like dynamic planning, the idea is indeed the dp idea.