Maximum sub array

Here I post a way to solve maximum sub array problem: 

The problem described like this: here is an array like [1,4,5,32,4,8,7,1,23,88], we should find a sub array such that the difference between tail and head of the sub array is maximum.

def getMaxSubarray(ls):
    a,b = ls[0],ls[0]
    ixl,ixr = 0,0
    summ = 0
    for i in range(1,len(ls)):
        if ls[i] > b:
            b = ls[i]
            ixr = i
            #print 'b: ',b,'and bix: ',ixr
        if ls[i] < a:
            summ2 = b - a
            if summ2>summ:
                summ = summ2
                fixl = ixl
                fixr = ixr
            a = ls[i]
            b = ls[i]
            ixl = i
            ixr = i
            #print 'a: ',a,' and ax: ',ixl,' and b: ',b,' and bx: ',ixr
    if (b - a) > summ:
        summ = b-a
        fixl = ixl
        fixr = ixr
    return(fixl,fixr,summ)

ls = [10,12,11,8.5,10.5,10.2,6.7,10.1,9.4,10.6,11.2,8,14,9,13,6.8,20,10.1,7.9,9.3,9,9.7]
getMaxSubarray(ls)
原文地址:https://www.cnblogs.com/vpegasus/p/6868367.html