Nine Chapters count judges Net-original website
http://www.jiuzhang.com/problem/31/
Topics
Given an array, two disjoint and contiguous sub-arrays A and B (positional continuous), satisfying |sum (A)-sum (B) | maximum (and the absolute value of the difference). For example [2,-1,-2, 1,-4, 2, 8], can be obtained a=[-1,-2, 1,-4], b=[2, 8], the maximum difference is 16.
Answer
The answer is to preprocess the maximum/minimum sub-array for each position to the left/right, and then enumerate the locations to find the maximum values in all Maxleft[i]-minright[i+1] and maxright[i+1]-minleft[i]. Preprocessing O (n), the enumeration divides the position O (n), the whole O (n).
Interviewer Angle
In the nine algorithm surface question 12 Maximum sub-range/sub-matrix, we introduced the O (N) time to obtain the largest sub-array (sub-range) method, in this topic, in fact, by enumerating the position (ab two array intermediate interval) to find the difference between the two array of the problem of the largest/ The problem with the least-small array. The most small array only needs to be calculated by using the maximum number of sub-arrays for the inverse number of each count.
Consider the subject carefully, in fact a and B arrays must be adjacent arrays, because the assumption that there is a number between A and B, if and C, if the c>0 can be added to the larger side of a and b to make the difference bigger, if c<0, then add the smaller side can also make the difference becomes larger if c= 0, adding a or B will not affect the result. Therefore, the answer can only be obtained by calculating the maximum/minimum continuous and to the right of each position to be taken to the left.
The maximum difference of the 31 sub-arrays in the nine-chapter algorithm surface test