[Classic Data Structure algorithm] (3) maximum sub-array and

Source: Internet
Author: User
1 /************************************** **********************************/
2 /*
3. Question:
4. Enter an integer array with a positive or negative number in the array. One or more consecutive integers in the array form a sub-array. Each sub-array has a sum.
5. Calculate the maximum value of the sum of all sub-arrays. The time complexity is O (n ).
6 For example:
7 The input array is 1,-2, 10,-4, 7, 2,-5, and the maximum sub-array is 3, 10,-4, 7, 2.
8. Therefore, the output is the sum of 18.
9
10 analysis:
11 you can use max_sum to save the sum of the current largest sub-array. Suppose the size of the array is N. Then the traversal starts from 0th; The sum of the current sub-array is sum.
12 In the initial state, max_sum =-INF (negative infinity) and sum = 0.
13 when I (0 <= I <= N) is traversed, sum + = I. If sum> max_sum, max_sum is assigned to sum. The sum value is not cleared, and the traversal continues. If sum is <0, sum = 0,
14 now you can consider: the maximum value is either on the left side of I, and it has been recorded in max_sum; or on the right side of I, then the sum value will be cleared, start computing and again.
15 is it possible to say that the maximum value includes both the left side of I and the right side of I? This is impossible.
16 because the left side of I is negative. If the sum on the left and right is the largest, the value on the right should be greater, and the two are in conflict. Therefore, this situation is impossible.
17 calculate max_sum first, and then judge whether the sum value is smaller than zero, including all negative cases.
18 */
19 /************************************** **********************************/
20
21 # include "stdafx. H"
22 # include <limits. h>
23
24
25 int calculatemaxsum (int * intarray, int count)
26 {
27 int ret = int_min;
28 int sum = 0;
29 for (INT I = 0; I <count; I ++)
30 {
31 sum + = intarray [I];
32 If (sum> RET)
33 {
34 ret = sum;
35}
36 IF (sum <0)
37 {
38 sum = 0;
39}
40
41}
42 return ret;
43
44}
45
46 int _ tmain (INT argc, _ tchar * argv [])
47 {
48 int intarray1 [] = {1,-2, 3, 10,-4, 7, 2,-5 };
49 printf ("% d \ n", calculatemaxsum (intarray1, 8 ));
50 int intarray2 [] = {-3,-2,-7,-4,-5,-2,-11 };
51 printf ("% d \ n", calculatemaxsum (intarray2, 8 ));
52 return 0;
53}

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.