[Leetcode] Maximum Subarray Maximum sub-array

Source: Internet
Author: User

Find the contiguous subarray within an array (containing at least one number) which have the largest sum.

For example, given the array [−2,1,−3,4,−1,2,1,−5,4] ,
The contiguous Subarray has the [4,−1,2,1] largest sum = 6 .

Click to show more practice.

More Practice:

If you had figured out the O (n) solution, try coding another solution using the divide and conquer approach, WHI CH is more subtle.

This problem let us seek the sum of the largest sub-array, and we have to use two methods to solve, respectively, is O (n) solution, but also useful division of the method divide and conquer approach, the time complexity of the solution is O (NLGN), then we first look at the O (n) solution, Define two variables res and TMP, where Res holds the final result to return, that is, the sum of the largest sub-arrays, TMP is a temporary variable, the initial value is the first number of the array, each traversing a number a[i], compared to the larger values in TMP + A[i] and a[i] into TMP, The larger values in RES and TMP are then stored in res, and so on until the complete array is traversed, the maximum number of sub-arrays can be found in res, the code is as follows:

Solution One

//O (n) SolutionclassSolution { Public:    intMaxsubarray (intA[],intN) {intres = a[0], TMP = a[0];  for(inti =1; I < n; ++i) {tmp= MAX (tmp +A[i], a[i]); Res=Max (res, TMP); }        returnRes; }};

The topic also asks us to divide the method divide and conquer approach to solve, this division method thought is similar to the binary search method, we need to divide the array into two, find out the sum of the largest sub-arrays on the left and the right, and then start scanning from the middle to the left, The maximum value is calculated by comparing the maximum value of the left and right sides to the largest one, the code is as follows:

Solution Two

//Divide and conquer approachclassSolution { Public:    intMaxsubarray (intA[],intN) {returnGetmaxsubarray (A,0N1); }    intGetmaxsubarray (intA[],intLeftintRight ) {        if(left >= right)returnA[left]; intMid = (left + right)/2; intLmax = Getmaxsubarray (A, left, mid-1); intRmax = Getmaxsubarray (A, Mid +1, right); intMmax = A[mid], TMP =A[mid];  for(inti = mid-1; I >= left; --i) {tmp+=A[i]; Mmax=Max (Mmax, TMP); } tmp=Mmax;  for(inti = mid +1; I <= right; ++i) {tmp+=A[i]; Mmax=Max (Mmax, TMP); }        returnMax (Mmax, Max (Lmax, Rmax)); }};

[Leetcode] Maximum Subarray 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.