Sword Point of Offer (Java Edition): Maximum and large contiguous subarray

Source: Internet
Author: User

Title: Enter an array of integers with positive and negative numbers in the array. One or more consecutive integers in an array make up a sub-array.

The maximum value for the and of all sub-arrays. Requires a time complexity of O (n)

For example, enter an array of {1,-2,3,10,-4,7,2,-5}

Seeing this topic, many people can think of the most intuitive method, which is to enumerate all the subarray of arrays and find out their and. An array of length n with a total of n (n+1)/2 sub-arrays. Calculates the N2 of all the sub-arrays, and the fastest also requires O (*.) time. Often the most intuitive approach will not be the best approach, and the interviewer will prompt us for a quicker approach.


Solution One: Example analysis of the pattern of the array:

We try to accumulate each number in the sample array one at a head-by-tail. Initialization and for 0. The first step plus the first number, at this time and 1. The next step is to add the number-2, and then programming-1. The third step plus the number 3. We note that the sum of the previous cumulative and 1, less than 0, if used-1 plus 3, the sum is 2, is smaller than the 3 itself. That is, the sub-array starting with the first number and the sum of the sub-arrays that begin with the third number. So we don't have to think about the first subarray, the accumulated sum, and also the discarded one.

We start with the third number again, and we get the sum of 3. Next fourth step plus 10, get and 13. Fifth Step Plus-4, and 9. We found that 4 is a negative number, so the cumulative-4 is smaller than the original. So we're going to save what we got before and 13, and it's probably the largest of the sub-arrays. The sixth step plus the number 7,9 plus 7 result is 16, at this time and than before the largest and 13 is also larger, the largest sub-array and updated from 13 to 16. The seventh step plus 2, the sum obtained is 18, and we also want to update the sum of the maximum sub-arrays. The eighth step plus the last number-5, because the resulting result is 13, less than the previous obtained maximum and 18, so the end of the largest subarray and 18, the corresponding sub-array is {3,10,-4,7,2}.

After analyzing the process, we use Java code to implement:

/** * Enter an array of integers with positive and negative numbers in the array. One or more consecutive integers in an array make up a sub-array. * The maximum value of all sub-arrays is evaluated. Requires time complexity O (n) */package swordforoffer;/** * @author Jinshuangqi * * August 8, 2015 */public class E31greatestsumofsubarrays {Pub  Lic Integer findgreatestsum (int[] arr) {if (arr.length ==0) return null; int greatest = 0x80000000;int Cursum = 0;for (int i = 0;i<arr.length;i++) {if (cursum <= 0) cursum = arr[i];elsecursum + = arr[i];if (Cursum >greatest) greatest = curSum;} return greatest;} public static void Main (string[] args) {int[] arr={-1,-2,-3,-10,-4,-7,2,-5}; E31greatestsumofsubarrays test = new E31greatestsumofsubarrays (); System.out.println (Test.findgreatestsum (arr));}}

Solution two: Applying dynamic programming method

We can also use the idea of dynamic programming to analyze this problem. If you use the function f (i) to denote the maximum and the number of sub-arrays ending with the first digit, then we need to find max[f (i)], where 0<=i<n. We can use the following recursive disclosure to ask F (i):


The meaning of this formula: when all the numbers in the sub-array ending in the i-1 number and less than 0 o'clock, if the number of the negative and the sum of the first, the result is smaller than the first number itself, so in this case, the sub-array ending with the number i is the first number itself. If the sum of all the numbers in the subarray ending in the i-1 number is greater than 0, the sum of all the numbers in the sub-array ending with the I-number is summed up with the first number.

Although we use recursive methods to analyze the problems of dynamic programming, we will eventually encode them based on loops.

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Sword Point of Offer (Java Edition): Maximum and large contiguous subarray

Related Article

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.