The maximum difference of the 31 sub-arrays in the nine-chapter algorithm surface test

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.