"The beauty of Programming" report: 2.14 The maximum value of the sum of the subarray of the array

Source: Internet
Author: User

Introduction

??

In this paper, we use five methods to solve this problem, from the initial n^3 complexity to the complexity of the back n , we use recursive formula, divide and conquer and dynamic programming method to reduce the complexity of the algorithm in one step.

??

Problem Solving Report

??

First of all, we can easily think of a solution is three-layer traversal, first the subarray must be a continuous string of values, equivalent to from the array of arrays of the subscript range 0~n-1 i and J, to calculate ARRA[I]~ARRAY[J] and so we can get the first solution

??

public static int Sol1 (int[] array) {

int lenght = Array.Length;

int result = Integer.min_value;

int tempsum = 0;

for (int i = 0; i < lenght; i++) {

for (int j = i; J < lenght; J + +) {

TempSum = 0;

for (int k = i; k <= J; k++) {

TempSum + = Array[k];

}

if (TempSum > result) {

result = TempSum;

}

}

}

return result;

}

??

Next, because Array[i]~array[j] and can be array[i]~array[j-1] and then add to array[j] , so we can reduce the above code a layer of loop, Thus reducing the complexity to n^2

??

public static int sol2 (int[] array) {

int lenght = Array.Length;

int result = Integer.min_value;

for (int i = 0; i < lenght; i++) {

int tempsum = 0;

for (int j = i; J < lenght; J + +) {

TempSum + = Array[j];

if (TempSum > result) {

result = TempSum;

}

}

}

return result;

}

??

has now been reduced to n^2 , but we do not use the method of divide and conquer, generally this dichotomy can reduce the complexity once again to Nlogn, then how to use it.

??

Consider dividing the given array of arrays (array[0]~array[n-1]) into two-segment arrays of equal length (array[0],..., array[n/2-1]) and (ARRAY[N/2],..., Array[n-1])

??

The respective maximal sub-segments of the two arrays are then calculated respectively, and the Maxleftbordersum and Maxrightbordersum are obtained.

??

The largest sub-segments of the original array (Array[0],..., array[n-1]) are divided into the following three cases:

??

A. (Array[0],..., array[n-1]) the largest sub-segments and the same as the largest sub-segments (array[0],..., array[n/2-1]) ;

B. (array[0],..., array[n-1]) the largest sub-segments and the same as the largest sub-segments (ARRAY[N/2],..., array[n-1]) ;

c. (Array[0],..., array[n-1]) the largest sub-segment spans between two elements array[n/2-1] to ARRAY[N/2].

??

corresponding to a and B Two problems are two identical sub-problems with a half scale, which can be obtained by recursion.

For C, you need to find the largest array of array[n/2-1] endings and s1= (Array[i],..., array[n/2-1]) and ARRAY[N/2] start and the largest segment and s2= (ARRAY[N/2],..., array[j]), then the maximum value for the third case is s1+s2.

??

public static int sol3 (int[] Array, int. left, int. right) {

if (left = right) {// only one element

if (Array[left] > 0)

return Array[left];

Else

return 0;

}

int start = left;

int mid = (left + right)/2;

int end = right;

int maxleftbordersum, maxrightbordersum;// maximum continuous sub-sequence values from middle to left and right, corresponding to C.

int leftbordersum, rightbordersum;

int maxleftsum = sol3 (array, start, mid);

int maxrightsum = sol3 (Array, mid + 1, end);

maxleftbordersum = 0;

leftbordersum = 0;

for (int i = Mid, I >= left; i--) {

Leftbordersum + = Array[i];

if (Leftbordersum > Maxleftbordersum)

Maxleftbordersum = Leftbordersum;

}

maxrightbordersum = 0;

rightbordersum = 0;

for (int i = mid + 1, I <= right; i++) {

Rightbordersum + = Array[i];

if (Rightbordersum > Maxrightbordersum)

Maxrightbordersum = Rightbordersum;

}

int max1 = maxleftsum > maxrightsum? Maxleftsum:maxrightsum;

int max2 = maxleftbordersum + maxrightbordersum;

return max1 > Max2? MAX1:MAX2;

}

??

The above has used the divide and conquer method to reduce the complexity from n^2 to Nlogn, considering that there is a way to reduce complexity that is dynamic planning, so we try to use the idea of dynamic planning to further reduce the complexity of

??

The idea of dynamic programming is to convert a large problem (n-element array) into a smaller problem (an array of n-1 elements).

??

We assume that result[0] is a neutron array with the array [0,1,... n-1] already found and the largest, that is, the largest subarray currently found. Sum[i] is a contiguous subarray that contains the element I and the largest. There are two choices for the i+1 element in an array:

??

A. As the first element of a new sub-array

B. Put in the largest sub-array sum[i-1] previously found.

??

public static int Sol4 (int[] array) {

int n = array.length;

int[] sum = new Int[n];

Int[] result = new Int[n];

Sum[0] = array[0];

Result[0] = array[0];

for (int i = 1; i < n; i++) {

Sum[i] = max (Array[i], array[i] + sum[i-1]);

// if a[i]>a[i]+sum[i-1], the first element of a new Subarray

// otherwise put in the largest sub-array previously found sum[i-1]

Result[i] = max (Sum[i], result[i-1]);

}

return result[n-1];

}

??

The above code actually sums the sum array and the result array can only use one variable to be able, does not actually use an array, then simplifies this point, can obtain the simplified version the final method, the complexity is n

??

static int sol5 (int[] array, int n) {

int sum = array[0];

int result = Array[0];

for (int i = 1; i < n; i++) {

sum = Max (Array[i], array[i] + sum);

result = max (sum, result);

}

return result;

}

"The beauty of Programming" report: 2.14 The maximum value of the sum of the subarray of the 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.