The maximum continuous subsequences and problems are very old interview questions. The best solution is O (n) Complexity. Of course, some of the smaller ones are worth noting. Here we will summarize three common solutions, focusing on the last O (n) solution. Note that in some questions, the maximum continuous subsequences and if they are negative, 0 is returned. In this question, the maximum continuous subsequences and do not return 0. If they are all negative, returns the maximum negative number.
Problem description
Obtain the maximum continuous subsequence sum in the array. For example, if the given array is a = {1, 3,-2, 4,-5}, the maximum continuous subsequence is 6, that is, 1 + 3 + (-2) + 4 = 6.
Solution 1-O (N ^ 2) Solution
Because the maximum continuous subsequence and it can only start from a position in the array 0 to n-1, We can traverse 0 to n-1 positions, calculates all consecutive subsequences starting from this position and the medium maximum value. The maximum value is obtained.
In more detail, it is to calculate the maximum continuous subsequence sum starting from position 0, and the maximum continuous subsequence sum starting from position 1... Until the maximum continuous subsequence sum starting from position N-1 is obtained, and the maximum and middle values of all these continuous subsequences are the answer.
Int maxsequence (INT arr [], int Len) {int max = arr [0]; // The initial maximum value is the first element for (INT I = 0; I <Len; I ++) {int sum = 0; // sum must be cleared for (Int J = I; j <Len; j ++) {// calculate the maximum continuous subsequence sum starting from position I. If it is greater than Max, update Max. Sum + = arr [J]; If (sum> MAX) max = sum ;}} return Max ;}
Solution 2-O (nlgn) Solution
This problem can also be solved through the partitioning method. The maximum continuous subsequence and the subsequence appear either in the left half of the array, or in the right half of the array, or across the left half of the array. Therefore, the maximum continuous subsequence and can be obtained by finding the maximum values in these three cases.
Int maxsequence2 (int A [], int L, int U) {If (L> U) return 0; If (L = u) return a [l]; int M = (L + u)/2;/* calculate the left half of the maximum continuous subsequence spanning left and right */INT Lmax = A [m], lsum = 0; for (INT I = m; I> = L; I --) {lsum + = A [I]; If (lsum> Lmax) Lmax = lsum ;} /* calculate the right half of the largest continuous subsequence spanning left and right */INT rmax = A [M + 1], rsum = 0; For (INT I = m + 1; I <= u; I ++) {rsum + = A [I]; If (rsum> rmax) rmax = rsum;} return max3 (Lmax + rmax, maxsequence2 (, l, m), maxsequence2 (a, m + 1, u); // returns the maximum value of the three}/* returns the maximum value of the three numbers */INT max3 (int I, Int J, int K) {if (I >= J & I> = k) return I; return max3 (j, k, I );}
Solution 3-O (n) Solution
There is also a better solution that only requires O (n) time. Because the maximum continuous subsequence and only the positions 0 ~ The end of a position in n-1. When traversing the I element, judge whether the continuous subsequence before it and whether it is greater than 0. If it is greater than 0, the maximum continuous subsequence ending with position I is the continuous subsequence and addition of element I and the front door. Otherwise, the maximum continuous subsequence ending with position I is the sum of element I.
Int maxsequene3 (int A [], int Len) {int maxsum, Maxhere; maxsum = Maxhere = A [0]; // The maximum value for initialization is a [0] For (INT I = 1; I <Len; I ++) {If (Maxhere <= 0) Maxhere = A [I]; // if the maximum consecutive subsequences at the preceding position and the values smaller than or equal to 0, the maximum continuous subsequences ending with the current position I will be a [I] else Maxhere + = A [I]; // if the maximum continuous subsequences at the preceding position and the number greater than 0, the maximum continuous subsequences ending with the current position I are the sum of them. If (Maxhere> maxsum) {maxsum = Maxhere; // update the maximum continuous subsequence and} return maxsum ;}