Enter an array of shapes with positive and negative numbers in the Array. One or more consecutive integers in an array make up a sub-array, each of which has a and. The maximum value for the and of all Sub-arrays. Requires a time complexity of O (n)

Source: Internet
Author: User

See this problem in the group, do it with python,

defFind (array): v_sum= Greatest =0 forAinchArray:v_sum+=a v_sum= 0ifV_sum < 0ElseV_sum Greatest= V_sumifV_sum > GreatestElseGreatestifV_sum = =0:greatest=array[0] forAincharray:greatest= AifGreatest < AElseGreatestPrintgreatestarr1= [1, 2,-4, 3, 3]find (arr1)

Idea: from left to right sliding scale, if the v_sum is positive, you can continue to add to the back of the element,

If the v_sum has been negative, it is necessary to clear the original v_sum to 0, if v_sum clear 0 and greatest is not one by one synchronous,

finally, comparing greatest and v_sum, it is important to note that if the list is full of negative values, then the value of greatest is to iterate over the following table to take the maximum Value.

Enter an array of shapes with positive and negative numbers in the Array. One or more consecutive integers in an array make up a sub-array, each of which has a and. The maximum value for the and of all Sub-arrays. Requires a time complexity of O (n)

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.