Maximum Value and extension of the sum of child arrays in the beauty of programming (can be connected at the beginning and end)

Source: Internet
Author: User

Case 1: the start and end connections are not allowed.

This situation is very common. The method is dynamic planning, and method 3 of the beauty of programming provides an understanding method. The code is provided here.

Int maxsubsum (vector <int> & Data) {int length = data. size (); Assert (length> = 0); int maxsum = data [length-1], startsum = data [length-1], begin = length-1, I; for (I = length-2; I> = 0; I --) {startsum = max (data [I], data [I] + startsum ); // If (startsum> maxsum) {begin = I; maxsum = startsum; // sum of the current largest sub-array} startsum = 0; For (; begin <length & startsum! = Maxsum; startsum + = data [begin], begin ++) cout <data [begin] <""; cout <Endl; return maxsum ;}

Scenario 2: Arrays can be connected at the beginning and end

There are two solutions to the problem:

1) The solution does not go through a [n-1] to a [0], that is, the normal request for the maximum value of the subarray and
2) The solution spans a [n-1] to a [0]

In 2nd cases, there are two possibilities (2.1) that contain the entire array; (2.2) that contains two parts, one starting from a [0] and the other ending with a [n-1, this is equivalent to deleting one or the smallest sub-array from array A. When the minimum value is 0, that is, all is not 0, it is the same as (2.1. I personally think that the case where the solution is from a [n-1] to a [0] must be greater than the case where the solution is not crossed. Therefore, the final result is the subarray of the sum of all elements minus the negative number of the absolute value. The Code is as follows:

Int maxsubsumendtoend (vector <int> & Data) {int length = data. size (); Assert (length> = 0); int minsum = data [length-1], startsum = data [length-1], allsum = data [length-1], begin = length-1, I, j; for (I = length-2; I> = 0; I --) {allsum + = data [I]; startsum = min (data [I], data [I] + startsum); If (startsum <minsum) {begin = I; minsum = startsum; // calculate the negative subarray with the maximum absolute value} If (minsum> 0) {minsum = 0; begin = 0;} startsum = 0; j = begin; fo R (; j <length & startsum! = Minsum; startsum + = data [J], j = (J + 1) % length); // locate the start position JFOR (I = J; I! = Begin; I = (I + 1) % length) cout <data [I] <""; cout <Endl; Return (allsum-minsum );}

If you have any questions, please correct them. Thank you.

Maximum Value and extension of the sum of child arrays in the beauty of programming (can be connected at the beginning and end)

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.