Four-Channel simple DP

Source: Internet
Author: User

DP class Topic Find sub-problem (state), and then find the transfer equation, OK

#DP#likes Matrixchain#According to both Point ' s distance to recurrenceclassSolution:#@return A string    defLongestpalindrome (self, s): Length=Len (s) P= [[0 forColinchRange (length)] forRowinchrange (length)] forIinchRange (len (s)): P[i][i]= 1if(i< (LENGTH-1) andS[i] = = s[i+1]): Maxlenth= 2P[i][i+1] = 1Start=I forIinchRange (3,length+1):             forJinchRange (length-i+1): K= j + i-1if(S[j]==s[k] andP[J+1][K-1] = = 1): Maxlenth=I p[j][k]= 1Start=Jreturns[start:start+Maxlenth]if __name__=='__main__': S=solution () test_str='ABCBA'    PrintS.longestpalindrome (TEST_STR)
#dp least number coins#one:overlapping Subproblem#two:optimal substructureclassSolution:#@return A string    defLeast_number_coin (self, S, v):#First initializationMin_number = [1000000]* (s+1) min_number[0]=0#and then,recurrence        #Time need O (n^2), and additional time O (n) to save the Subproblem ' s temp solution         forIinchRange (1,s+1):             forJinchRange (len (v)):PrintI,v[j]if(v[j]<=i and(Min_number[i-v[j]]+1 <Min_number[i]): Min_number[i]= Min_number[i-v[j]]+1PrintMin_numberreturnMin_number[s]if __name__=='__main__': S=solution () money= 11v= [1,3,5]    PrintS.least_number_coin (MONEY,V)
#DP#Time O (n^2), addtional O (n) to save temp resultclassSolution:#@return A string    defLIS (self,v):Printv D= [1]*(Len (v))PrintD forIinchRange (len (v)): forJinchRange (i):Printi,jif(V[j]<=v[i] andD[j]+1>D[i]): D[i]= D[j]+1PrintDPrintMax (d)if __name__=='__main__': S=solution () v= [5,3,4,8,6,7] S.lis (v)
#DPclassSolution:#@return A string    defmax_number_apples (self, apples, N, m):PrintApples s= [[0 forColinchRange (m)] forRowinchrange (n)] forIinchrange (n): forJinchRange (m):if(i==0 andj==0): S[i][j]=Apples[0][0]elif(i==0 andJ>0): S[i][j]= s[i][j-1]+Apples[i][j]elif(j==0 andI>0): S[i][j]= S[i-1][j] +Apples[i][j]Else:                    if(s[i-1][j]>s[i][j-1]): S[i][j]= S[i-1][j] +Apples[i][j]Else: S[i][j]= S[i][j-1] +Apples[i][j]PrintsreturnS[n-1][m-1]if __name__=='__main__': S=solution () n= 3m= 4Apples= [[0 forColinchRange (m)] forRowinchrange (n)] K= 1 forIinchrange (n): forJinchRange (m): Apples[i][j]=k K= k + 1s.max_number_apples (apples,n,m)

One:initialization (Initialize)

Two:recurrence (Recursive)

Polynomial time, additional storage space required

Four-Channel simple DP

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.