Alibaba Cloud pen questions: Dynamic Planning for the largest sub-section and Problems

Source: Internet
Author: User

The maximum child segment and the Dynamic Programming solution of the problem. A number is given to find the maximum field and?

For example, array A, the number of integers is N, and B is the secondary array.

A: 2 3 -7 6 4 -5

B: 2 5 -2 6 10 5

The maximum sub-segment is 10.

 

This topic uses the idea of dynamic planning, so that B [J] is the largest subsection and ending with J, so there are:

B [J] = max {B [J-1] + A [J], a [J]}

Therefore, when finding the value of B [J], you only need to check whether B [J-1] is greater than zero, if greater than zero, then B [J] = B [J-1] + A [J]; if less than zero, B [J] = A [J].

In this case, there are n B Values in total. Save the maximum value.

 

Code:

Int maxsum (INT [] A, int N)

{

Int sum = 0;

Int B = 0;

For (INT I = 0; I <n; I ++)

{

If (B> 0)

B + =;

Else

B =;

If (B> sum)

Sum = B;

}

Return sum;

}

 

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.