Dynamic programming-The sum of the maximal contiguous subarray (Python implementation) & solving the maximum continuous product string (Python implementation)

Source: Internet
Author: User

def maxsum (self,array,n):    sum=array[0]    result=array[0] for    i in range (0,n):        if sum<0:            sum=a[ I]        else:            sum=sum+a[i]            start1=i        if Sum>result:            result=sum            end=i            start=start1    Print Result,start,end

The above is a dynamic programming idea: Suppose Sum[i] represents the largest continuous string ending with the element I, then sum[i]=max{sum[i-1]+a[i],a[i]}, judging the size of the element in parentheses, it becomes the judgment sum[i-1] is greater than 0

Maximum continuous product dynamic planning: Max represents the product of the largest continuous string ending in A, Min represents the smallest, then max=max{a[i],max[i-1]*a[i],min[i-1]*a[i]}min=min, because the maximum minimum may be just the symbol.

def maxmulti (self,array,n):    maxa=[]    mina=[]    maxa[0]=mina[0]=array[0]    value=maxa[0]    for I in range (0,n):        Maxa[i]=max (A[i],maxa[i-1]*a[i],mina[i-1]*a[i])        mina[i]=min (a[i],maxa[i-1]*a[i],mina[ I-1]*a[i])        Value=max (value,maxa[i])    print value

  

Dynamic programming-The sum of the maximal contiguous subarray (Python implementation) & solving the maximum continuous product string (Python implementation)

Related Article

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.