This is very commonAlgorithmSee classical algorithm books (or classical interview books) such as the beauty of programming and the pearl of programming :)). There are a lot of discussions and implementations on this issue on the Internet. I would like to write down my understanding here. Some people may have written it before, but after all, it is something I think about and I should record it.
Problem: A one-dimensional array with N integer elements (a [0], a [1]..., A [n-1]). Of course, this array has many sub-arrays (N * n) and calculates the sum of the largest sub-arrays.
Classic solution:
1Maxsofar =02Maxendinghere =03ForI = [0, N)4Maxendinghere = max (maxendinghere + A [I],0)5Maxsofar = max (maxendinghere, maxsofar)
Here I am writing the pseudocode in programming Pearl.Code, I think this pseudocode and Bentley's explanation best interpret this problem.
Bentley writes: the key to understanding this program is the variable maxendinghere. This algorithm is a typical dynamic planning algorithm, which is described as follows:
When calculating the maximum sub-array of the first I number and the time, we first record the sum of the maximum sub-array of the first I-1 number maxi-1. When we encounter a new number A [I], if the addition of a [I] makes the sum of a subarray and the maximum number of subarrays greater than the number of the previous I-1, update the sum of Maxi = the sub-array. Otherwise, Maxi = maxi-1. next, we only need to consider which sub-array can be added and added by adding a [I? Of course, it is a child array ending with the number of I-1. Therefore, in the calculation of the first I Number of the maximum sub-array, in addition to record the number of I-1 before the maximum sub-array, we should also record the number of I-1 ending with the maximum sub-array. The algorithm is as follows:
Suppose we already know that the maximum sub-array of the previous I-1 array is a [J],..., A [K], which is sumk, And we record the largest subarray ending with I-1 as a [m],..., A [I-1], which is for Sumi-1, apparently, Sumi-1 <= sumk.
If a [I]> 0 encounters the number of I, the Sumi-1 + A [I] may be greater than sumk. so we update the sum of the largest sub-array of the number of I before: max {Sumi-1 + A [I], sumk }.
Then we should also update the sum of the largest sub-arrays ending with I: SUMI = max {Sumi-1 + A [I], 0 }. that is, if the Sumi-1 + A [I] is greater than 0, the maximum sub-array ending with I is a [m],..., A [I-1], a [I]. Otherwise, if the Sumi-1 + A [I] is less than or equal to 0, the sum of the Child array is not as good as the sum of 0 of an empty child array, reset the Child number to null, the sum is 0.
This classic problem has a very similar variant: that is, if an array is also given, write a maximum value to find the discontinuous subarray and the maximum value, that is, any adjacent two elements in the subarray, in the original number group, they must be non-adjacent. Take the Array {1,-2, 3, 5,-1, 2} as an example, and the maximum discontinuous subarray is {1, 5, 2 }, the Return Value of the function is 8.
With the answers to the first question, the second question should be very simple. Let's take it into consideration, assuming that we already know the maximum value of the discontinuous sub-array and the number of the first I-1, when the number of I is added, under what circumstances will this discontinuous subarray change?
After the number of I is added, it cannot be connected to the maximum nonconsecutive subarray ending with the I-1, which violates the discontinuous rules. So we need to consider the end of the maximum I-2 and the maximum discontinuous sub-array, set it as a Sumi-2. when the Sumi-2 + A [I] is greater than the maximum number of nonconsecutive subarrays and ending with the number of I-1, we update the discontinuous sub-array of the array consisting of the number of I before and the maximum value is Sumi-2 + A [I]. The algorithm code is:
1 For(IntI = 2; I <length; I ++){2Maxendingi = max (maxendingi_2 +Array [I], maxendingi_2, maxendingi_1 );3IntTMP =Maxendingi_2;4Maxendingi_2 =Max (maxendingi_1, maxendingi_2 );5Maxendingi_1 = max (TMP + array [I], array [I]);6}
Maxendingi indicates the sum of the maximum discontinuous subarrays of arrays composed of the first I.
Maxendingi_2 records the sum of the largest and largest discontinuous subarrays ending with a I-2, initialized to array [1].
Maxendingi_1 records the sum of the largest discontinuous subarray ending with a I-1, initialized to array [2].
If the array length is less than 3, it can be processed separately.
References:
[1] Jon Bentley programming Pearl p81
[2] interview questions: Find the maximum sum of discontinuous subarrays