Beauty of programming Reading Notes 2.14-maximum value of the sum of sub-Arrays

Source: Internet
Author: User

Http://blog.csdn.net/pipisorry/article/details/39083281

Problem:

1. A one-dimensional array composed of N integer elements, and the maximum values of the elements and in all its sub-arrays.

2. if the beginning and end of the array are adjacent, that is, the sub-array a [I],..., A [n-1], a [0],..., A [J] exists, and the total elements and maximum values of all its sub-arrays are obtained.



Solution 1:

/* O (N ^ 2) traversal algorithm */static int maxsubarraysum1 (int * a, int a_len) {int max_sum = int_min, sum; For (INT I = 0; I <a_len; I ++) {// the largest sum of the subarrays starting with I, traversing all the conditions sum = 0; For (Int J = I; j <a_len; j ++) {sum + = A [J]; If (sum> max_sum) max_sum = sum ;}} return max_sum ;}

Solution 2:

/* O (nlogn) sharding algorithm */static int maxsubarraysum2 (int * a, int low, int high) {// The maximum sub-segment between a [low] a [High] And if (low> = high) // when only one element is left, return itself is the maximum value return a [low]; int mid = (high + low)/2; // The sum of the largest array ending with a [Mid]: int sum = 0, max_sum_left = int_min; For (INT I = mid; I> = low; I --) {sum + = A [I]; If (sum> max_sum_left) max_sum_left = sum;} int max_sum_right = int_min; // sum of the largest array segment starting with a [Mid + 1] = 0; For (INT I = Mid + 1; I <= high ; I ++) {sum + = A [I]; If (sum> max_sum_right) max_sum_right = sum;} int max_sum = max_sum_left + max_sum_right; int max_sub_left = maxsubarraysum2 (, low, mid); // maximum sub-segment of the Left segment of the array and INT max_sub_right = maxsubarraysum2 (A, Mid + 1, high ); // maximum sub-segment and INT max_sub = max_sub_left> max_sub_right? Max_sub_left: max_sub_right; max_sum = max_sum> max_sub? Max_sum: max_sub; // maximum sub-segment of the array and return max_sum ;}

Solution 3:

/* O (n) DP algorithm */static int maxsubarraysum3 (int * a, int N) {int * Start = (int *) malloc (sizeof (A [0]) * n); // start [I] is the largest subarray containing a [I] Starting from I and int * All = (int *) malloc (sizeof (A [0]) * n ); // All [I] is the largest array from I and START [n-1] = A [n-1]; all [n-1] = A [n-1]; for (INT I = N-2; I> = 0; I --) {start [I] = start [I + 1] + A [I]> A [I]? Start [I + 1] + A [I]: A [I]; // start [I] = max {A [I], start [I + 1] + A [I]} All [I] = start [I]> All [I + 1]? Start [I]: All [I + 1]; // All [I] = max {start [I], all [I + 1]} return all [0];}/* O (n) DP algorithm (O (1) space) */static int maxsubarraysum4 (int *, int N) {int start = A [n-1]; int all = A [n-1]; for (INT I = N-2; I> = 0; I --) {start = start + A [I]> A [I]? Start + A [I]: A [I]; All = Start> All? Start: All;} return all ;}

Solution 4:
/* O (n) optimal algorithm * // * the optimal solution is to scan the array only once, so the time is O (n ). Suppose x1, x2,..., xt is the optimal solution. Obviously, the sum of I <= T, x1, x2,..., and XI cannot be negative. Otherwise, if we cut this segment, we can get a larger value, which is a conflict of the optimum. This means that the segment prefix of the optimal solution cannot be negative. In other words, if the sum of a segment is negative, it cannot be part of the optimal solution. At the beginning, set the current segment to start from X1 and leave it blank. We start to search forward from the array, add the number we encounter to the current segment S, and record the largest sum we encounter. This process continues until a certain number of Xi is added, so that the sum of S is negative, then the S is cleared, and the next element of Xi is the start of the current segment, continue to search forward. Repeat this process until the end of the array. During implementation, you do not need to maintain the set S and pair it with and every time. Instead, you only need to maintain the sum of the current segment. When new elements are added to the current segment, the sum of the updated segments is updated; when a new segment is started, the sum of the 0 segments is cleared. */Static int maxsubarraysum (int * a, int N) {int sum = 0, max_sum = int_min; For (INT I = 0; I <n; I ++) {sum + = A [I]; If (sum> max_sum) max_sum = sum; If (sum <0) // when the prefix is <0, sum accumulation and sum = 0 can be removed;} return max_sum;}/* O (n) optimal algorithm (record left and right boundary) */static int maxsubarraysum5 (int * a, int N) {int sum = 0, max_sum = int_min; int max_low = 0, max_high = 0; // optimal subarray left and right boundary int low = 0; // The first subscript of the subarray that is not <0 prefix for (INT I = 0; I <n; I ++) {sum + = A [I]; If (sum> max_sum) {max_sum = sum; max_high = I; max_low = low;} If (sum <0) {// when the prefix is <0, sum accumulation and sum = 0 can be removed; Low = I + 1 ;}} printf ("max_low = % d, max_high = % d \ n ", max_low, max_high); Return max_sum ;}

Test:

//************************************** **************************************** * ******* // * The beauty of programming 2.14 -- calculates the maximum value of the sum of the sub-arrays of arrays (Microsoft Yayan 2006) pipi *///********************************** **************************************** * ************/# include <stdio. h> # include <assert. h> # include <malloc. h> # include <limits. h> int main () {assert (freopen ("BOP \ maxsubarraysum. in "," r ", stdin); INT cases; // Number of test cases scanf (" % d ", & cases); While (cases --) {int N; // Number of array elements in each case scanf ("% d", & N); int * A = (int *) malloc (sizeof (INT) * n ); for (INT I = 0; I <n; I ++) scanf ("% d", & A [I]); printf ("% d \ n ", maxsubarraysum1 (A, n); printf ("% d \ n", maxsubarraysum2 (A, 0, n-1); printf ("% d \ n ", maxsubarraysum3 (A, n); printf ("% d \ n", maxsubarraysum4 (A, n); printf ("% d \ n", maxsubarraysum (, n); printf ("% d \ n", maxsubarraysum5 (A, n);} fclose (stdin); Return 0 ;}


Test Case:

4
7-2 5 3-6 4-8 6
6 1-2 3 5-3 2
6 0-2 3 5-1 2
5-9-2-3-5-3


Answer:
8 [1-2]
8 [2-3]
9 [2-5]
-2 [1-1]



From:

Http://blog.csdn.net/pipisorry/article/details/39083281

Ref:

Http://blog.csdn.net/pipisorry/article/details/39048485


Beauty of programming Reading Notes 2.14-maximum value of the sum of sub-Arrays

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.