[Algorithm Summary-DP] calculate the maximum sum of sub-Arrays

Source: Internet
Author: User

I'm about to graduate.AlgorithmAnd the data structure. Summarize the project-related information. First, review the previous knowledge, and second, record the learning points to facilitate later learning.

 
Description: given an integer array, there are positive and negative numbers in the array. One or more consecutive integers in the array form a subarray. each subarray has an sum, returns the maximum subarray sum of the given array. It also becomes the "maximum sub-segment and" problem. The time complexity is O (n ).

This is a simple DP problem. There are many ideas about the algorithm, but if the time complexity is to be met, it is O (n ). General algorithms cannot be reached.

DP solves this problem by Scanning Arrays, recording the sum of the current array and the largest subarray that has been recorded. If the sum of the current record is already a negative number, discard it and reset the current array and the value of the current element. Otherwise, compare the sum of the current array and the sum of the largest sub-array that has been recorded. If the sum is greater than the largest, update the largest sum. Scan and complete the statistics to meet the requirements of the questions.

The algorithm is implemented as follows:

# Include <stdio. h> # include <assert. h> # include <stdlib. h> int maxsubsum (int * a, int N) {assert (n> 0); int max = 0, cursum = 0; For (INT I = 0; I <N; I ++) {If (cursum> 0) {cursum + = A [I];} else {cursum = A [I];} If (cursum> MAX) {max = cursum;} return Max;} int main () {int A [] = {1,-, 10,-, 2,-5 }; printf ("% d \ n", maxsubsum (A, 8); System ("pause"); Return 0 ;}

For further consideration, if not only the largest sum is obtained, but also the start and end indexes of the largest field are required. So what should we do.

The idea of the algorithm is the same. The only difference is that we need to add two variables start and end to record the start and end indexes. In addition, when the value changes, you can obtain the starting and ending index of the maximum sub-segment after scanning.

The algorithm is as follows:

# Include <stdio. h> # include <assert. h> # include <stdlib. h> int maxsubwithindex (int * a, int N, Int & START, Int & End) {assert (n> 0); Start = END = 0; int max = 0, cursum = 0; For (INT I = 0; I <n; I ++) {If (cursum> 0) {cursum + = A [I];} else {start = I; cursum = A [I];} If (cursum> MAX) {end = I; max = cursum ;}} return Max ;} int main () {int A [] = {1,-2, 3, 10,-4, 7, 2,-5}; int start = 0, end = 0; printf ("OK: % d \ n ", maxsubwithindex (A, 8, start, end); printf (" starts: % d ends: % d \ n ", start, end ); system ("pause"); Return 0 ;}

Further expansion: For a two-dimensional matrix, there is also a largest sub-matrix, so that the sum of the sub-matrix is the largest. Think about it: Is this question similar to the previous largest field and question?

Before coding, let's draw a picture:

If the sum of each column is regarded as an element, is the problem similar to the problem of finding the maximum field and in one dimension? We only need to scan the matrix sequentially, find the sum of each sub-matrix, and then compare it with the largest sum. Finally, we can find the sum of the largest sub-matrix.

AlgorithmCodeAs follows:

# Include <stdio. h> # include <assert. h> # define n 3int maxsum (int * a, int N) {assert (n> 0); int I, sum = 0, max = 0; for (I = 0; I <n; I ++) {If (sum> 0) {sum + = A [I];} else {sum = A [I];} if (sum> MAX) {max = sum;} return Max;} int maxsubmatrix (INT ** A, int M, int N) {int I, J, K, max = 0, sum = 0; int * B = new int [N]; for (I = 0; I <m; I ++) {for (k = 0; k <n; k ++) {B [k] = 0 ;}for (j = I; j <m; j ++) {for (k = 0; k <n; k ++) {B [k] + = A [J] [k];} sum = maxsum (B, n); If (sum> MAX) {max = sum ;}}return Max ;}main () {int M, N; while (scanf ("% d", & M, & N) = 2) {int ** A = new int * [m]; for (INT I = 0; I <m; I ++) {A [I] = new int [N]; for (Int J = 0; j <n; j ++) {scanf ("% d ", & A [I] [J]) ;}} printf ("% d \ n", maxsubmatrix (a, m, n); For (INT I = 0; I <m; I ++) delete a [I]; delete ;}}

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.