Sword refers to the offer face question (Java version): The maximum and __ of contiguous sub Arrays (Java version)

Source: Internet
Author: User
Title: Enter an integer array with positive and negative numbers in the array. One or more consecutive integers in an array form a child array.

The maximum value of the and of all child arrays is evaluated. Requires a time complexity of O (n)

For example, the input array is {1,-2,3,10,-4,7,2,-5}

Seeing this topic, many people can think of the most intuitive way, that is, to enumerate all the arrays of the array and to find their and. An array of length n, with a total of n (n+1)/2 sub arrays. Calculates all of the N2 of the array, and the fastest need O () time. Often the most intuitive approach is not the optimal approach, and the interviewer will prompt us for a quicker approach.


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

We try to accumulate each number in the sample array from the beginning. Initialize and 0. The first step plus the first number, at this time and 1. The next step is to add the number-2, and then the programming-1. The third step plus the number 3. We note that as the previous cumulative sum of 1, less than 0, that if used-1 plus 3, the resulting sum is 2, smaller than the 3 itself. That is, the number of the child array starting with the first digit is less than the and of the sub array starting with the third number. So we don't have to consider from the first sub array, the previous cumulative and also be discarded.

We start over with a third number, and then we get the sum of 3. Next step Fourth plus 10, get and 13. Fifth Step Plus-4, and 9. We found that-4 is a negative, so the sum-4 gets and is smaller than the original. So we're going to save the previous and 13, which is probably the largest of the array. The sixth step plus the number 7,9 plus 7 results is 16, at this time and the largest and 13 is larger than before, the largest array of and from 13 update to 16. Seventh Step plus 2, cumulative and 18, at the same time we also want to update the sum of the maximum sub array. The eighth step plus the last number-5, because the result is 13, less than the previous maximum and 18, so the final largest sub array and 18, the corresponding child array is {3,10,-4,7,2}.

After analyzing the process, we implement it in Java code:

/**
 * Enter an integer array with positive and negative numbers in the array. One or more consecutive integers in an array form a child array.
 * Find the maximum value for the and of all the child arrays. Require time complexity O (n)
 * * *
package swordforoffer;

/**
 * @author Jinshuangqi
 * *
 * August 8, 2015/public
class E31greatestsumofsubarrays {
	
	public 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];
			else
				cursum + = 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 the Dynamic programming method

We can also apply the idea of dynamic programming to analyze this problem. If the function f (i) is used to represent the maximum and the max[f of the substring ending with the number I, then we need to find the 0<=i<n. We can use the following recursive publicity to find F (i):


The meaning of this formula: when the number of all the digits in the array ending with the i-1 number is less than 0 o'clock, if you add this negative to the number of I, the result is smaller than the first number itself, so the sub array ending with number I is the first number itself. If the sum of all the digits in the i-1 end of the number is greater than 0, the sum of all the digits in the array ending with the I number is obtained with the first number.

Although we use recursive methods to analyze the problem of dynamic programming, but ultimately will be based on the loop to encode.

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.