The beauty of programming 2.14 calculates the maximum value of the sum of the array sub-arrays, the beauty of 2.14

Source: Internet
Author: User

The beauty of programming 2.14 calculates the maximum value of the sum of the array sub-arrays, the beauty of 2.14

This is a very likely question during the interview. Let me take it for example. Two of the five companies interviewed asked this question. It can be seen that, this topic is very classic.

It is not difficult to solve the problem. I am familiar with the following two solutions:

1. Always join. The condition for terminating the current sequence is that the sum of the join values is negative.

Because, after a number is added with a negative number, it is certainly not as big as the original value. Therefore, this is meaningless. In the end, we can use this idea to obtain the following solution.

Function declaration:

ll DutMaxSeqSubArray_1(int*, int);

typedef long long ll;

Source code:

/* Very common solutions for maximum continuous subarrays */bool _ DutMaxSeqSubArray = false; ll DutMaxSeqSubArray_1 (int * A, int size) {if (! A | size <= 0) {_ DutMaxSeqSubArray = true; return-1;} ll result = 1 <31; ll currentSum = 0;/* loop through the array, when the sum is negative, start the next round of addition */for (int I = 0; I <size; ++ I) {if (currentSum <= 0) currentSum = A [I]; elsecuritysum + = A [I]; if (result <currentSum) result = currentSum;} return result ;}

2. Idea of Dynamic Planning: continuous subarrays have the largest and must end with a certain value.

No matter how many consecutive sub-arrays exist or how large the sum is, it always ends with a number, and that number must also appear in the array. Therefore, the following recursive formula is obtained:

The sum of the maximum continuous subarrays ending with the current number is:

PData [I] = pData [I-1] + A [I] | A [I]

Therefore, the following solution can be obtained:

Function declaration:

ll DutMaxSeqSubArray_2(int*, int);

Source code:

/* Dynamic planning solution */ll DutMaxSeqSubArray_2 (int * A, int size) {if (! A | size <= 0) {_ DutMaxSeqSubArray = true; return-1;} ll result = 1 <31; ll currentSum = 0; for (int I = 0; I <size; ++ I) {/** the idea is that the maximum continuous subarray must end with an array element. Therefore, * The maximum continuous sum of the entire * array */currentSum = DutMax <ll> (currentSum + A [I], A [I]); result = DutMax <ll> (result, currentSum);} return result ;}

Template functions used:

template <typename T>T DutMax(T data1, T data2){return data1 > data2 ? data1 : data2;}



The largest sub-array of an array and

This is a classic algorithm;
(1) initialize the sum = 0;
(2) starting from the first element of the array, if the value is greater than or equal to 0, it is added to sum, that is, sum + = a [I]; If <0, it is switched to (3)
(3) JUDGE a [I] ~ The sum of a [n-1]> = 0, if so, and add it to sum. else writes down the sun value and repeats it from a [I + 1] (1)

C language an integer array of all sub-arrays with the largest value

# Include <stdio. h>
Int MaxSum3 (int * A, int n) {// optimization solution time O (n) Space O (1)
Int nStart = A [n-1];
Int nAll = A [n-1];
For (int I = n-2; I> = 0; I --){
If (nStart <0)
NStart = 0;
NStart + = A [I];
If (nStart> nAll)
NAll = nStart;
}
Return nAll;
}
Int main ()
{
For (int I = 0; I <N; I ++ ){

Scanf ("% d", & A [I]);
}
Printf ("% d", MaxSum3 (A, N ));
Return 0;
}

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.