Clear Test Instructions:
- Sub-arrays are contiguous
- Simply require and, do not need to return the exact location of the Subarray
- The element is an integer, and the array may contain positive integers, 0, negative integers
Example:
Array [1,-235,-328
Clear Test Instructions
At first I thought that even the loop would not solve the problem, such as the element 3,5 in the above example to make up the "maximum" subarray. Not to mention if 3 and 5 are far away. Of course the latter exacerbates the complexity of the problem.
Therefore, the first step, clear "problem" meaning is an important link to solve.
Example code 1:
intMaxsum (intAintN) { intMaximum =-1; intsum; for(inti =0; I < n; ++i) {Sum=0; for(intj = i; J < N; ++j) {Sum+=A[j]; if(Sum >maximum) {Maximum=sum; } } } returnmaximum;}
But through the above algorithm of O (n^2) complexity, the solution of the problem is really obtained.
In fact, it implies that I consider the situation, and it is all. What is required is the and of a contiguous subarray. Then all the cases can be categorized:
For an array of 1 elements, (a), then it must be itself; for 2 elements, an array of (A, b), except for the first case, the only combination, (the first element, the second element), which is (a, b) an array of 3 elements, (A,B,C), Can also be listed, such as: A or B or C, a combination of (b) or (B,C) (where AC does not meet the continuous requirements) for 4 elements, (and so on)
The above is a similar to the primary mathematics education often used in incomplete induction, through this analysis, you can come to a result, that is, the largest sub-array can be classified according to a standard, that is, the number of sub-array elements. Coupled with an additional constraint condition, you can use two loops to solve the problem. For an array consisting of n elements:
(1,2,3 , ..., N)
A continuous sub-array is a case of M species (poor math, make up the presentation):
m = cn1 + (CN2-discontinuous) + (CN3-discontinuous) + ... + (cnn-discontinuous)
Looking back at example code 1, both inside and outside the loop, it is true that all of the above combinations are covered.
Represented by the following table in the array:
maximum to save the largest subarray and, sum, save the element combination of a seed array. The following are the values of the elements, respectively, with array element subscripts: When i=0, sum represents0,0+1, (0+1) +2, (0+1+2) +3, ... , (0+1+2+3+ ... + N-1) +n when I=1, sum is expressed separately .1,1+2, (1+2) +3, ... , (1+2+3+ ... + N-1) +N
... When i=n-1, sum is divided to denote N-1, N-1+ n when i=n, sum to indicate n
All of the above combinations are continuous.
For the example at the beginning of the article, the following table in the largest subarray (3,5) for the elements in the original array is (2,3), that is, when the n=5,i=2,j=3 gets the maximum value 8
Make the appropriate output statement in the appropriate place to observe the entire operation:
I J sum MAXIMUM0 0 1 1 -1 2 2-3 7 4 4 5 6 1- 2 2 1 + 3 6 4 3 ( 5 5) 2 3 7 2 3 8 82 4 5 5 7, 3 5, 4 2.5 4 84 4 -3 5 -1 5 2 8
Divide and conquer
Each problem can be decomposed into sub-problems of halving the size of two problems, plus a traversal algorithm.
intMaxintXinty) { return(x > Y)?x:y;}intMaxsum_demo2 (intAintN) { intNstart = a[n-1]; intNall = a[n-1]; for(inti = n2; I >=0; --i) {Nstart= Max (A[i], Nstart +A[i]); Nall=Max (Nstart, Nall); } returnNall;}
In the same way, the results are printed in the right place:
I a[i] nstart nAll4 -3 -1 5 5 3 8 Bayi -2 6 1 7 8
Dynamic planning, divided treatment to see.
The maximum value of the sum of the sub-arrays of the array