Maximum sub-array problem

Source: Internet
Author: User

To find the largest and most contiguous sub-arrays

To find the largest and most large array of sub-arrays
Title Description:
Enter an array of shapes with positive and negative numbers in the array.
One or more consecutive integers in an array make up a sub-array, each of which has a and.
The maximum value for the and of all sub-arrays.

For example, the input array is 1,-2, 3, 10,-4, 7, 2,-5, and the largest subarray is 3, 10,-4, 7, 2,
So the output is the and 18 of the subarray.

----------------------------------I am a graceful dividing line----------------------------------

Dynamic programming complexity of O (n)

    Set Test[i] is a contiguous subarray that ends with the first element and the largest. Assuming that for element I, all the lengths of the sub-arrays that end with its preceding elements have been evaluated, then the contiguous subarray that ends with the first element and the largest is actually either the i-1 of the first element and the largest contiguous subarray plus the element, or it contains only the I-element, or max[i] = max ( MAX[I-1] + test [i], test [i]). The choice can be made by judging whether Max[i-1] + a[i] is greater than a[i], which is actually equivalent to judging whether max[i-1] is greater than 0. Since each operation requires only the previous result, it is not necessary to keep all the previous results as normal dynamic planning, only the last one, so the algorithm has very little time and space complexity.

public static int Dongtai () {

/**

* Initialization Parameters

* Leftsum: The maximum sub-array value with TEST[I-1] as the tail;

* Max: The maximum number of sub-array values obtained when the Traverse is saved;

*/

int leftsum = 0, max=0;

for (int i = 0; i < test.length; i++) {

Leftsum = leftsum + test[i] > test[i]? Leftsum + test[i]: test[i];

max = leftsum > Max? Leftsum:max;

}

return Max;

}



----------------------------------I am a graceful dividing line----------------------------------

The degree of complexity of the method of division and Treatment is O (NLGN)

public class Max Sub-array problem {


public static int[] Test = {1,-2, 3, 10,-4, 7, 2,-5};

public static void Main (string[] args) {

Maximum Subarray Problem demo = new Maximum sub-array problem ();

System.out.println (Demo. Maxsubsum (0,test.length-1));

}


public static int Maxsubsum (int. Left,int right) {

/**

* Initialization Parameters

* Maxleft: Left maximum substring value, Maxright: Right maximum substring value;

* Maxcentertoleft: From the middle to the left plus the maximum value, Maxcentertoright: From the middle to the right plus maximum;

* Maxcentertoleftsum: From the middle to the left plus, Maxcentertorightsum: from the middle to the right plus;

*/

int maxleft=0,maxright=0;

int maxcentertoleft=0,maxcentertoright=0;

int centertoleftsum=0,centertorightsum=0;

int center= (left + right)/2;//Middle Point

Boundary

if (left = = right) {

return Test[left];

}

The left maximum substring value and the right maximum substring value are obtained;

Maxleft = Maxsubsum (Left,center);

maxright= maxsubsum (center+1,right);

From center to left plus maximum value

for (int i = center, I >= left; i--) {

if (Maxcentertoleft < centertoleftsum +test[i]) {

Maxcentertoleft = Centertoleftsum +test[i];

}

Centertoleftsum + = Test[i];

}

From center to right plus maximum value

for (int j = center+1, J < right; J + +) {

if (Maxcentertoright < centertorightsum +test[j]) {

Maxcentertoright = Centertorightsum +test[j];

}

Centertorightsum + = Test[j];

}

Left maximum, right max, maximum across medium max

int max1 = Maxleft>maxright? Maxleft:maxright;

int max2 = Max1>maxcentertoleft+maxcentertoright? Max1:maxcentertoleft+maxcentertoright;

Return MAX1>MAX2? MAX1:MAX2;

}

}

----------------------------------I am a graceful dividing line----------------------------------

This article from the "Damenmai Learning Road" blog, reproduced please contact the author!

Maximum sub-array problem

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.