Recently re-learning algorithms and data structures, recommending a book: Data Structures and algorithm analysis in Java 3rd
The following four algorithms are derived from this book
The four largest sub-sequences and algorithms:
Problem description
Given (possibly negative) integer a (1), A (2) 、...... a (n), the maximum value of a (1) +a (2) +......+a (j) is obtained. For convenience, if all integers are negative, the maximum subsequence and is 0.
That is: In a series of integers, find a continuous number of integers, which are the sum of the largest number of integers.
The first kind: exhaustive all possible, due to nested three-layer for loop, run time O (n^3)
PackageDemo1; Public classDemo1 { Public Static voidMain (string[] args) {int[] A = {-2, 4,-3, 5, 7,-1, 8, 1 }; intMax =MaxSubSum1 (a); SYSTEM.OUT.PRINTLN (max); // + } Private Static intMAXSUBSUM1 (int[] a) {intMaxsum = 0; for(inti = 0; i < a.length; i++) { for(intj = i; J < A.length; J + +) { intThissum = 0; for(intK = i; K <= J; k++) {thissum+=A[k]; } if(Thissum >maxsum) {Maxsum=thissum; } } } returnmaxsum; }}
Second: Simplification on the first basis, removal of a For loop, run time O (n^2)
PackageDemo1; Public classDemo2 { Public Static voidMain (string[] args) {int[] A = {-2, 4,-3, 5, 7,-1, 8, 1 }; intMax =maxSubSum2 (a); SYSTEM.OUT.PRINTLN (max); // + } Private Static intMAXSUBSUM2 (int[] a) {intMaxsum = 0; for(inti = 0; i < a.length; i++) { intThissum = 0; for(intj = i; J < A.length; J + +) {thissum+=A[j]; if(Thissum >maxsum) {Maxsum=thissum; } } } returnmaxsum; }}
The two algorithms are similar in nature, and the two algorithms behind them will greatly increase efficiency.
The Third Kind: the thought of solving here completely changed, time just O (NLOGN)
It divides this group into the first half and the second half, and then deals with the two parts separately (divide-and-conquer method)
It is obvious that the maximal subsequence and must be one of the three in the previous or the latter or the middle, and then the recursive loop is used to calculate
Note: The code is the shorter the better, but the algorithm is not necessarily, this algorithm may be very long, but compared to the first two algorithms it is more excellent
The code is as follows:
PackageDemo1; Public classDemo3 { Public Static voidMain (string[] args) {int[] A = {-2, 4,-3, 5, 7,-1, 8, 1 }; intMax =maxSubSum3 (a); SYSTEM.OUT.PRINTLN (max); // + } Private Static intMAXSUBSUM3 (int[] a) {//recursive initialization of parameters returnMaxsumrec (A, 0, a.length-1); } Private Static intMaxsumrec (int[] A,intLeftintRight ) { //determine if there is only one element if(left = =Right ) { if(A[left] > 0) { returnA[left]; } Else { return0; } } intCenter = (left + right)/2; intMaxleftsum =Maxsumrec (A, left, center); intMaxrightsum = Maxsumrec (A, center + 1, right); //Left processing intMaxleftbordersum = 0; intLeftboardersum = 0; for(inti = center; I >= left; i--) {leftboardersum+=A[i]; if(Leftboardersum >maxleftbordersum) {Maxleftbordersum=leftboardersum; } } //right-side processing intMaxrightboardersum = 0; intRightboardersum = 0; for(inti = center + 1; I <= right; i++) {rightboardersum+=A[i]; if(Rightboardersum >maxrightboardersum) {Maxrightboardersum=rightboardersum; } } //returns the maximum value returnMath.max (Math.max (Maxleftsum, maxrightsum), Maxleftbordersum +maxrightboardersum); }}
The fourth way: the best algorithm: O (N)
This is a clever, hard-to-come-up, requiring programmers with deep programming skills to think
PackageDemo1; Public classDemo4 { Public Static voidMain (string[] args) {int[] A = {-2, 4,-3, 5, 7,-1, 8, 1 }; intMax =MaxSubSum4 (a); SYSTEM.OUT.PRINTLN (max); // + } Private Static intMAXSUBSUM4 (int[] a) {intMaxsum = 0; intThissum = 0; for(inti = 0; i < a.length; i++) {thissum+=A[i]; if(Thissum >maxsum) {Maxsum=thissum; } Else if(Thissum < 0) {thissum= 0; } returnmaxsum; } return0; }}
Getting Started with algorithms: Four Algorithms (Java) for maximum subsequence