Java implementations: Finding the maximum and (array) of Subarray

Source: Internet
Author: User

To find the maximum and (array) of the Subarray

Topic:
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. Requires a time complexity of O (n).

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


Problem Solving Ideas:

I think the user is concerned about two data, one is a sub-array of the and, there is a sub-array specific elements, so we can encapsulate a sub-array class Subarray, Members have sub-data start index, end index, as well as sub-array and.

For this question, I look at other blog answers when a sub-array length is passed into the parameter, the maximum value of all sub-arrays of size is obtained. But above the topic, there is no explanation for the size entry. We still provide solutions to these two methods.

First, the specified array length is passed to size. This method is relatively simple to implement. First we have a variable maxarray of the largest subarray subarray, and then we initialize a Subarray object of size, named CurrentArray. When we traverse the user element, we first remove the first element from the CurrentArray and then put the user element, and when the new CurrentArray is greater than Maxarray, update the Maxarray.

The second type has no incoming array length of size. We just have to make sure that the sum of the data before index is plus the index index data >0, then the index index corresponds to the maximum value is contributed. When the sum of the data before index is combined with the sum of index indexes, the data before index (including index) is useless and <0. When we get the above array, we need to reverse the loop once and remove the negative values from the end of the queue to ensure that the sub-array is the largest.

The code is as follows:

public class Maxsubarray {private list<integer> valueList = Null;public Maxsubarray () {valueList = new arraylist< Integer> ();} public Maxsubarray (int size) {valueList = new arraylist<integer> (size);} public void Add (int value) {Valuelist.add (value);} Public subarray maxsubarray (int size) {if (Valuelist.isempty ()) {return null;} Initializes an array of current operations and a maximum array of int currentsize = Math.min (Valuelist.size (), size); Subarray CurrentArray = new Subarray (0, 0, 0); for (int i = 0; i < currentsize; i++) {Currentarray.endix = I;currentarray . Sum + = Valuelist.get (i); Subarray Maxarray = new Subarray (currentarray);//calculate for (int i = size; I < valuelist.size (); i++) {//) if the current array does not have an empty space reserved, start from the Remove the element and make sure to reserve an empty if (Currentarray.endix-currentarray.startix + 1 >= size) {currentarray.sum-= Valuelist.get ( Currentarray.startix); currentarray.startix++;} Currentarray.endix = i;currentarray.sum + = Valuelist.get (i); if (Currentarray.sum > Maxarray.sum) {maxArray.copy ( CurrentArray);}} return Maxarray;} Public Subarray MaxsuBarray () {if (Valuelist.isempty ()) {return null;} Loop backward, as long as the sum before position I is less than the value of position I, then includes the sub-array drop of position I. Subarray Lastmaxarray = new Subarray (0, 0, valuelist.get (0)); Subarray resultarray = new Subarray (Lastmaxarray); for (int i = 1; i < valuelist.size (); i++) {int value = Valuelist.get ( i); if (resultarray = = null) {resultarray = new Subarray (i, I, value); continue;} if (resultarray.sum + value < 0) {if (Resultarray.sum > Lastmaxarray.sum) {lastmaxarray.copy (resultarray);} Resultarray = null;continue;} Resultarray.endix=i;resultarray.sum + = Valuelist.get (i);} From the back-forward loop, as long as the sum after position I is less than the value of position I, then the array is discarded, including the position I. for (int i = Resultarray.endix; I >= Resultarray.startix; i--) {int value = Valuelist.get (i); if (value < 0) {Resultarr Ay.endix = i-1;resultarray.sum-= value;continue;} else {break;}} return resultarray;} public class subarray{private int startix;private int endix;private long sum;public subarray (subarray copy) {copy]; Public subarray (int startix, int endix, long sum) {This.startix = startix;thIs.endix = Endix;this.sum = sum;} public void copy (Subarray copy) {This.startix = Copy.startix;this.endix = Copy.endix;this.sum = Copy.sum;} public int Getstartix () {return This.startix;} public int Getendix () {return this.endix;} Public long Getsum () {return this.sum;}}}

Go to dinner first, and then write JUnit tests.


Java implementations: Finding the maximum and (array) of 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.