"The sword means offer" Q31: Group of contiguous sub-arrays Yamato

Source: Internet
Author: User

for a brief analysis, see:http://blog.csdn.net/shiquxinkong/article/details/17934747

def findgreatestsumofsubarray (array, index = None): cursum = 0maxSum = 0#return maxsum without the start and end indexif in Dex = = None:for x in array:cursum = max (cursum, 0) cursum + = Xmaxsum = Max (maxsum, cursum) return Maxsum#return maxsum and T He start and end indexelse:curs = -1cure = -1maxs = -1maxe = -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

There are two version numbers that are overloaded with index parameters, one that only returns the maximum number of sub-segments, but sometimes we may also be asked to return the starting range that gets the maximum sub-segment.

Although the above code does not look like dynamic programming, the idea is actually DP.


"The sword means offer" Q31: Group of contiguous sub-arrays Yamato

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.