"The sword refers to the offer" face question 31-the biggest and problem of the continuous sub array questions _ Face questions

Source: Internet
Author: User
Topic Description:

Enter an array of integers with positive and negative numbers in the array. One or more consecutive integers in an array form an array of all the child arrays and the maximum value of the time complexity of O (N). Topic Analysis:

For this topic, most of us can suddenly think of: I can list all the arrays of an array, and then sum, select the largest and is the desired result. If you do this, the number of child arrays is n (n+1)/2 (n is the length of an array). And to find the complexity of the sum of the array is not O (1), so the time complexity of such a method must be greater than O (n*n), unreasonable solution.

Correct solution:

If you give a plastic array: {1,-2,3,4,-2,5,-4};
Define variable: Maxsum (largest and) initialized to 0,cursum (and the current sub-array) initialized to 0.
Processing number No. 0 subscript 1: plus 1 operation, Cursum is 1,1>0, so maxsum = 1.
Handle number 1th Subscript-2: Add-2 operation. Cursum = 1-2 = -1;maxsum = 1.
Deal with number 2nd subscript 3: Discard the front and (cursum<0). Cursum = 3.cursum>maxsum.maxsum = 3.
Handle number 3rd subscript: plus 4.curSum = 3+4 = 7.cursum>maxsum.maxsum = 7.
Handle number 4th subscript: Add -2.cursum = 7-2 = 5.maxSum = 7 (unchanged)
Handle number 5th subscript: plus 5.curSum = 5+5 = 10.cursum>maxsum.maxsum = 10.
Handle number 6th subscript, plus -4.cursum = 6.maxSum = 10 (invariant).
Therefore the largest and is 10. Code implementation:

int findmaxsum (int arr[],int n)
{
    assert (arr && n);
    int cursum = 0;
    int maxsum = 0;
    for (int i = 0; i < n; ++i)
    {
       if (Cursum < 0)
           cursum = arr[i];
       else
           cursum + = Arr[i];
       if (Cursum > Maxsum)
           maxsum = cursum;
    }
    return maxsum;
}

We know that the written program must have a certain degree of fault tolerance: for example, the number of array elements is an illegal number, indicating that the pointer to the array is null. Although I use assertions to judge in a program, it seems not a legitimate solution. Before the simulation implementation of atoi () We are using global variables to mark, here still use this method. The complete code is given below:

#include <iostream>
using namespace std;
BOOL Isinvalid = false;//illegal input
int findmaxsum (int arr[],int n)
{
    if (arr = NULL | | n <= 0)
    {
       Isinval id = true;
       return 0;
    }
    int cursum = 0;
    int maxsum = 0;
    for (int i = 0; i < n; ++i)
    {
       if (Cursum < 0)
           cursum = arr[i];
       else
           cursum + = Arr[i];
       if (Cursum > Maxsum)
           maxsum = cursum;
    }
    return maxsum;
}
int main ()
{
    //int arr[] = {1,-2,3,10,-4,7,2,-5};
    int arr[] = {1,-2,3,4,-2,5,-4};
    int ret = findmaxsum (arr,7);
    int ret = findmaxsum (arr,0);
    if (isinvalid)
    {
       cout<< "illegal input" <<endl;
       return 0;
    }   
    cout<<ret<<endl;
    System ("pause");
    return 0;
}

Of course there are other solutions to this problem (the sword refers to the offer)-dynamic planning.

Here the train of thought and the idea is actually the same, so the code is similar.
On the problem of the maximal and the continuous sub array here we go.

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.