Chapter III Progressive Basic O () .... Commonly used functions% and//convert Stirling approximate formula Fibonacci number fourth chapter divided governance strategy: decomposition (recursion)--solving (recursive bottom)--3 ways to combine solution recursion: 1: Substituting Method (substitution method): guessing one (by experience)--Mathematical Inductive method • 2: Recursive tree method: Drawing Tree P31 "3rd edition Chinese" p51-> recursion-proof 3: The Main method: fast, some places can not be involved, recursive type is not easy to write 4.1 the largest array problem division method: 1. A[low, mid] 2.a[mid+1, high] 3. Contains mid middle (think left and right respectively to traverse the combination to find the maximum)
ImportdecimaldefFind_max_crossing_subarray (A,low,mid,high): Left_sum=Decimal. Min_emin sum=0 forIinchRange (mid,low-1,-1): Sum= Sum +A[i]ifSum >Left_sum:left_sum=sum Max_left=I right_sum=Decimal. Min_emin sum=0 forIinchRange (Mid + 1,high+1): Sum= Sum +A[i]ifSum >Right_sum:right_sum=sum M ax_right=Ireturn(Max_left,max_right,left_sum +right_sum)defFind_maximum_subarray (a,low,high):ifHigh = =Low :return(Low,high,a[low])Else: Mid= (low + high)//2(left_low,left_high,left_sum)=Find_maximum_subarray (A,low,mid) (right_low,right_high,right_sum)= Find_maximum_subarray (a,mid+1, High) (cross_low,cross_high,cross_sum)=Find_max_crossing_subarray (A,low,mid,high)ifLeft_sum >= Right_sum andLeft_sum >=cross_sum:return(left_low,left_high,left_sum)elifRight_sum >= Left_sum andRight_sum >=cross_sum:return(right_low,right_high,right_sum)Else: return(cross_low,cross_high,cross_sum)if __name__=='__main__': A= [13,-3,-25,20,-3,-16,-23,18,20,-7,12,-5,-22,15,-4,7] Temp= Find_maximum_subarray (A,0,len (A)-1) Print(temp)" "========= restart:f:/python/algorithms/4_1_find_maximum_subarray.py ========= (7, Ten) O (n*n) python 3.5.1win7 and pseudo-code are almost identical--! The only question to be aware of is Python starting from 0" "
Linear level improvement O (n) Exercise: 4.1-5 It is easy to understand that when we add a positive number, it increases; when we add a negative number, it decreases. If the current sum is a negative number, then this and in the next summation should be discarded and re-zeroed, otherwise this negative will reduce the next and
defFindgreatestsumofsubarray (A):ifA:ncursum= Ngreatestsum =0#Ncursum Save now and Ncreatestsum save the largestCurstart = Curend =0 Start= end =0 forIinchRange (len (A)):#Traverse aNcursum + =A[i] Curend=IifNcursum < 0:#Discard plus a positive number for negative number = minus the positive numberLittle Ncursum=0 Curstart= Curend = i + 1ifNcursum > Ngreatestsum:#refreshes the largest arrayNgreatestsum =ncursum Start=Curstart End=Curend#are negative traversal to find the maximum negative number ifNgreatestsum = =0:ngreatestsum=A[0] Start= end =0 forIinchRange (1, Len (A)):ifA[i] >Ngreatestsum:ngreatestsum=A[i] Start= end =Ireturn[Start,end,ngreatestsum]Else: return []if __name__=="__main__": A= [13,-3,-25,20,-3,-16,-23,18,20,-7,12,-5,-22,15,-4,7] Temp=Findgreatestsumofsubarray (A)Print(temp)" "$ Python3 4_1_5_find_maximum_subarray.py [7, Ten, 43]o (n) Python 3.4.3+ubuntu 15.10" "
Reference: Http://www.wutianqi.com/?cat=515&paged=6
http://www.cnblogs.com/chinaxmly/archive/2012/10/10/2718621.htmlhttp://blog.csdn.net/yelbosh/article/details/7558981
Introduction to Algorithms chapter III and fourth Python