[Offoffoff] Q31: large group of Continuous sub-Arrays

Source: Internet
Author: User

Brief analysis see: http://blog.csdn.net/shiquxinkong/article/details/17934747

Def FindGreatestSumOfSubArray (array, index = None): curSum = 0 maxSum = 0 # return maxSum without the start and end indexif index = None: for x in array: curSum = max (curSum, 0) curSum + = xmaxSum = max (maxSum, curSum) return maxSum # return maxSum and the start and end indexelse: curS =-1 curE =-1 maxS =-1 maxE =-1for I in range (len (array): if curSum <= 0: curSum = array [I] curS = icurE = ielse: curSum + = array [I] curE = iif curSum> maxSum: maxS = curSmaxE = curEmaxSum = curSumreturn maxSum, maxS, maxE

Here, we use the index parameter to overload two versions. One of them only returns the maximum child segment and the other, but sometimes we may be asked to return the start range and get the maximum child segment.

Although the Code above does not look like dynamic planning, the idea is indeed the dp idea.


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.