def maxsum (self,array,n): sum=array[0] result=array[0] for i in range (0,n): if sum<0: sum=a[ I] else: sum=sum+a[i] start1=i if Sum>result: result=sum end=i start=start1 Print Result,start,end
The above is a dynamic programming idea: Suppose Sum[i] represents the largest continuous string ending with the element I, then sum[i]=max{sum[i-1]+a[i],a[i]}, judging the size of the element in parentheses, it becomes the judgment sum[i-1] is greater than 0
Maximum continuous product dynamic planning: Max represents the product of the largest continuous string ending in A, Min represents the smallest, then max=max{a[i],max[i-1]*a[i],min[i-1]*a[i]}min=min, because the maximum minimum may be just the symbol.
def maxmulti (self,array,n): maxa=[] mina=[] maxa[0]=mina[0]=array[0] value=maxa[0] for I in range (0,n): Maxa[i]=max (A[i],maxa[i-1]*a[i],mina[i-1]*a[i]) mina[i]=min (a[i],maxa[i-1]*a[i],mina[ I-1]*a[i]) Value=max (value,maxa[i]) print value
Dynamic programming-The sum of the maximal contiguous subarray (Python implementation) & solving the maximum continuous product string (Python implementation)