Dynamic planning solves the problem of "maximum sub-array"

Source: Internet
Author: User

TODO: What exactly is a dynamic plan?

Ref:http://www.cnblogs.com/waytofall/archive/2012/04/10/2439820.html

I only consider how to produce larger subgroups and:

Assuming that the first node is processed:

1. Consider whether the I node can make subgroups and grow larger

A) If the I node is greater than 0, then the maximum subgroup and is the array that was previously obtained and the I-node value

b) If I node is less than 0 (equal to 0, the value can take I, also can not take I, it does not matter), when the sum of the largest sub-array and is the front of the child Group and

2. Then consider if the maximum value should be updated

A) If the current subgroup and is greater than the maximum and, then the update

int//Dynamic programming method
Get_max_sub (int a[], int len)
{
int max_sum, cur_sum;
int i;

Max_sum = Cur_sum = A[0];

i = 0;
while (++i < Len) {
if (Cur_sum > 0) {
Cur_sum + = A[i];
} else {
Cur_sum = A[i];
}

if (Cur_sum > Max_sum) {
Max_sum = Cur_sum;
}
}

return max_sum;
}

II refinement consider the front and back boundaries of the Subarray

1. Because the sub-array and is negative, the maximum value is the I node, then the left edge is moved to the I node.

2. When I node is negative, (thinking mined area!!) when you move to the right? Of course, the sub-array and the larger, then moved (the child and the larger, the sub-array expanded)

int//Dynamic programming method
Get_max_sub (int a[], int len, int &lt, int &rt)
{
int max_sum, cur_sum;
int i;

Max_sum = Cur_sum = A[0];
lt = rt = 0;

i = 0;
while (++i < Len) {
if (Cur_sum > 0) {
Cur_sum + = A[i];
} else {
Cur_sum = A[i];
lt = i;
}

if (Cur_sum > Max_sum) {
Max_sum = Cur_sum;
RT = i;
}
}

return max_sum;
}

Summarize:

1. The depth of thinking is a little deeper, from less and more

2. Get rid of the thinking inertia and avoid the minefield.

When considering the maximum Subarray, consider whether the center is a historical sub-array and whether it is a positive or negative (that is, the sub-array and is the current first I node or the first node plus the previous and then), rather than just consider adding the I-node idea array is larger or smaller (intuitive/inertial thinking into the mined area)

Dynamic planning solves the problem of "maximum sub-array"

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.