Programming method: Interview and algorithm insights (maximum contiguous subarray and)

Source: Internet
Author: User

all the content comes from the programming method: Interview and algorithm experience book, the implementation is written by themselves using JavaTitle 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 of all sub-arrays, requiring a time complexity of O (n).

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

Analysis and Solution method one

To find the maximum subarray of an array and, I think the most intuitive and barbaric approach is that three for loop three-layer traversal, the number of each subarray in the array, and finally find out the maximum value of these sub-arrays. Make Currsum[i, ..., j] the sum of the first element of the array A to the J element and (wherein 0 <= i <= J < n), Maxsum is the sum of the largest contiguous subarray that is ultimately obtained.

And when it's all negative, we can let the program return 0, or let the program return the largest negative number, here, we let the program return the largest negative number.

The reference code is as follows:

/** To find the maximum subarray of an array and, I think the most intuitive and barbaric approach is that three for loop three-layer traversal, * to find the number of each subarray in the array and, ultimately, the maximum value of these sub-arrays.     * Make Currsum[i, ..., j] the sum of the first element of an array A to the J element and (wherein 0 <= i <= J < n), * maxsum is the sum of the maximal contiguous subarray that is ultimately obtained.     * And when all is negative, we can let the program return 0, you can also let the program return the largest negative number, here, we let the program return the largest negative number. */     Public Static intSolution1 (int[] arr) {        intMaxsum = arr[0];  for(inti=0;i<arr.length;i++)        {             for(intj=i;j<arr.length;j++)            {                intCurrsum = 0;  for(intk=i;k<j;k++) {currsum+=Arr[k]; } maxsum=Math.max (Maxsum, currsum); }        }        returnmaxsum; }

The time complexity of this method is O (n^3).

Solution Two

In fact, when we make currsum the and of the current largest subarray, Maxsum is the same as the maximum number of sub-arrays to be returned, and when we scan backwards,

    • There are two options for the j+1 element: either put in the previously found Subarray, or as the first element of the new sub-array;
      • If Currsum plus the current element a[j] is not less than a[j], then Currsum plus a[j], or currsum re-assigned to the next element, that is Currsum = A[j].
    • Also, when Currsum > maxsum, update maxsum = currsum, otherwise keep the original value, not updated.

That

currSum = max(a[j], currSum + a[j])maxSum = max(maxSum, currSum)

For example, when the input array is 1,-2, 3, 10,-4, 7, 2,-5, then the corresponding changes for Currsum and Maxsum are:

    • currsum:0 1-1 3 13 9 16 18 13
    • Maxsum:0 1 1 3 13 13 16 18 18

The reference code is as follows:

/** Solution two * In fact, when we make currsum for the current largest subarray of the and, Maxsum for the last to return the largest subarray of the and, when we scan backwards, * There are two options for the j+1 element: either put in the previously found sub-array, or as a new sub-array of the first     * If currsum plus the current element a[j] is not less than a[j], then Currsum plus a[j], or currsum re-assigned to the next element, that is Currsum = A[j].     * At the same time, when Currsum > maxsum, then update maxsum = currsum, otherwise keep the original value, not updated. * i.e. * Currsum = max (A[j], currsum + a[j]) * maxsum = max (Maxsum, currsum)*/     Public Static intSolution2 (int[] arr) {        intMaxsum = arr[0]; intCurrsum = 0;  for(inti=1;i<arr.length;i++) {currsum= Math.max (Arr[i], currsum +Arr[i]); Maxsum=Math.max (Maxsum, currsum); }        returnmaxsum; }

Programming method: Interview and algorithm insights (maximum contiguous subarray and)

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.