First, the more simple algorithm
Algorithm idea: We determine each sub-segment and start position, respectively, the first, the second, the third ... Nth, and then calculates the sub-segments of each position from this position onwards to this position, and updates the largest sub-segments and records.
Complexity of Time: O (n^2)
Algorithm implementation (Java):
Packagecom. Third;ImportJava.util.*; Public classmain3{ Public Static intMAXSUM2 (inta[]) { intnowsum=0;//used to record the accumulated value from the specified position to the current position intmaxsum=0;//used to record the current largest sub-segment and for(inti=0;i<a.length;i++) {nowsum=0; for(intj=i;j<a.length;j++) {nowsum=nowsum+A[j]; if(nowsum>maxsum) {//Update Max sub-segments andmaxsum=nowsum; } } } returnmaxsum; } Public Static voidMain (string[] args) {intA[]={4,-3,5,-2,-1,2,6,-2}; System.out.println (MaxSum2 (a)); }}
Second, divide and conquer the Law (recursion)
Algorithm idea:
The maximum sub-segment and the dividing array are divided into two parts, and the maximum sub-segments and three cases are present in the thought of divide and conquer:
1. Maximum sub-segments and appearing on the left side
2. Maximum sub-segments and appearing on the right side
3. Maximal sub-segments and spans in the left and right segments are obtained by comparing the size of the maximum sub-segment and
Complexity of Time: O (NLOGN)
Algorithm implementation (Java):
Packagecom. Third;/** Through the thought of dividing the maximum sub-section and dividing the array into two parts, the maximum sub-segments and the existence in three cases: * 1. The maximum child segment and appears on the left side * 2. The maximum child segment and appears on the right side of * 3. Max sub-segment and span in left*/ Public classMain { Public Static intMaxsumrec (int[]a,intStartintend) { if(Start==end) {//This is the recursive function exit. if(a[start]>0){ returnA[start]; }Else{ return0; } } intMaxleftsumrec=maxsumrec (A,start, (start+end)/2);//calculates the maximum field of the left half and intMaxrightsumrec=maxsumrec (A, ((Start+end)/2) +1,end);//calculates the right half of the maximum sub-segment and//calculates the maximum sub-segment and the case in the middle intLeftmaxmark=0; intLeftsum=0; for(intI= (start+end)/2;i>=0;i--) {leftsum=leftsum+A[i]; if(leftsum>Leftmaxmark) {Leftmaxmark=leftsum; } } intRightmaxmark=0; intRightsum=0; for(intI= ((start+end)/2) +1;i<=end;i++) {rightsum=rightsum+A[i]; if(rightsum>Rightmaxmark) {Rightmaxmark=rightsum; } } intmaxmidsumrec=leftmaxmark+Rightmaxmark; //compare three cases in which case is the largest sub-segment and intmaxsum=Maxleftsumrec; if(maxsum<Maxrightsumrec) {Maxsum=Maxrightsumrec; } if(maxmidsumrec>maxsum) {Maxsum=Maxmidsumrec; } returnmaxsum; } Public Static voidMain (string[] args) {intA[]={4,-3,5,-2,-1,2,6,-2}; System.out.println (Maxsumrec (A,0,7)); } }
Three, dynamic programming algorithm
Algorithm idea:
Using the idea of dynamic programming to solve the maximal sub-segments and problems:
By iterating over this array element, the timer updates the maximum sub-segment and,
If the current cumulative number is negative, discard directly, reset to 0, and then iterate through the accumulation.
Time complexity: O (N)
Algorithm implementation (Java):
Packagecom. Third;/** This is the use of the idea of dynamic planning to solve the maximum sub-segments and problems: * By iterating through the array element, the time to update the maximum sub-segment and, if the current cumulative number is negative, discard directly, reset to 0, and then go through the accumulation. */ Public classmain1{ Public Static intMAXSUBSUM1 (int[]a] { intmaxsum=0;intNowsum=0; for(inti=0;i<a.length;i++) {nowsum=nowsum+A[i]; if(nowsum>maxsum) {//Update Max sub-segments andmaxsum=nowsum; } if(nowsum<0) {//Discard when current summation and negative, reset to 0Nowsum=0; } } returnmaxsum; } Public Static voidMain (string[] args) {intA[]={4,-3,5,-2,-1,2,6,-2}; System.out.println (MaxSubSum1 (a)); }}
Several algorithms for finding maximal sub-segments