Python----Dynamic Planning

Source: Internet
Author: User

Can not give up treatment, every day to improve!!

When do you use dynamic planning?

1. Finding the optimal solution to a problem
2. Large problems can be broken down into sub-problems, sub-problems and overlapping smaller sub-problems
3. The optimal solution of the whole problem depends on the optimal solution of the sub-problem (state transition equation)
4. Analyze the problem from the top down and solve the problem from the bottom up
5. Discussion of the underlying boundary issues

Example 1: Cutting rope problems

Title: give you a rope of length n, please cut the rope into M segment (m and n are integers, n>1 and m>1) the length of each piece of rope is recorded as k[0],k[1],..., k[m]. What is the maximum possible product of k[0]k[1]...*k[m]? For example, when the length of the rope is 8 o'clock, we cut it to a length of three 2,3,3, the maximum product is 18.

Idea: F (n) =max{f (i) f (n-i)}, think and implement is 2 methods, think of the time is recursive, when the implementation is from the bottom to the top.

Implementation: 1 Meters The most 1, 2 meters max is 2, 3 meters max is 3, 4 meters Max is 4, and so on, and so on, to seek the largest cutting of n meters

Algorithmic complexity O (n2)

#-*-coding:utf-8-*defmaxcutstring (length):#these three lines represent the length of the input rope, when the cutting action occurs, the maximum product        ifLength < 2:                return0ifLength = = 2:                return1ifLength = = 3:return2#The rope is constantly cut, and when cut to a length of 1,2.3, you cannot continue cutting and return directly to thearr=[0,1,2,3]#record the maximum product of a rope length of I time Arr[i]         forIinchRange (4,length+1): Maxs=0 forJinchRange (1,i/2+1): Mult=arr[j]*arr[i-J]ifmaxs<Mult:maxs=mult arr.append (MAXS)returnArr[length]PrintMaxcutstring (8)
View Code

Example 2: Maximum consecutive children and

Ideas:

Implementation: Maxtmp record temporary children and, each number of encountered continuously accumulate, when the maxtmp is negative, empty, starting from the next number, the new accumulation, when the accumulated number is greater than maxsum, assign the value to Maxsum

Complexity: O (N)

#-*-coding:utf-8-*#!usr/bin/pythondefmaxsum (lists): Maxsum=0 maxtmp=0 forIinchRange (len (lists)):ifmaxtmp<=0:maxtmp=Lists[i]Else: Maxtmp+=Lists[i]ifMaxtmp >Maxsum:maxsum=maxtmpreturnmaxsumlists=[1,3,-3,4,-6,5]PrintMaxsum (lists)
View Code

There is also a brute force solution, double traversal, Complexity O (n2)

#-*-coding:utf-8-*#!usr/bin/pythondefmaxsum (lists): Maxsum=0 forIinchRange (len (lists)): Maxtmp=0 forJinchRange (I,len (lists)): Maxtmp+=Lists[j]ifMaxtmp >Maxsum:maxsum=maxtmpreturnmaxsumlists=[1,3,-3,4,-6,5]PrintMaxsum (lists)
View Code

Example 3: Putting apples

把M个同样的苹果放在N个同样的盘子里,允许有的盘子空着不放,问共有多少种不同的分法?(用K表示)5,1,1和1,5,1 是同一种分法。

思路:f(m,n) =f(m,n-1)+f(m-n,n) 

设f(m,n) 为m个苹果,n个盘子的放法数目,则先对n作讨论,

When n>m: There must be n-m a plate is always empty, remove them on the number of Apple methods to be placed no effect. i.e. if (n>m) f (m,n) = f (m,m)

When n<=m: Different methods of release can be divided into two categories:

    1、有至少一个盘子空着,即相当于f(m,n) = f(m,n-1); 

2. All plates have apples, which is equivalent to taking an apple from each plate, without affecting the number of different methods, i.e. f (m,n) = f (m-n,n).

The total number of put apples is equal to the sum of the two, that is F (m,n) =f (m,n-1) +f (m-n,n)

Description of the recursive export conditions:

1. When n=1, all apples must be placed on a plate, so return 1;

     2.当没有苹果可放时,定义为1种放法;

Two routes of recursion, the first n will gradually reduce, will eventually reach the exit n==1;

 第二条m会逐渐减少,因为n>m时,我们会return f(m,m) 所以终会到达出口m==0

# !usr/bin/python def f (m,n):     if or n==1):        return 1    if m<N:        return f (m,m)     Else :         return F (m,n-1) +f (M-n,n) lines=map (int,raw_input (). Strip (). Split ())Print f (lines[0],lines[1])
View Code

example four: Frog jumping step problem

1. If frogs can jump 1 levels at a time, they can jump 2 levels at a time.. Q. How many ways to jump on the nth step? idea: F (n) =f (n-1) +f (n-2) The nth level can only be skipped by the n-1 level and the n-2 level frog
#-*-conding:utf-8-*#Recursive Solutiondeff (n):ifN==1:        return1elifn==2:        return2Else:        returnF (n-1) +f (n-2)PrintF (8)#from bottom to top solutiondefF2 (n): Arr=[0,1,2]     forIinchRange (3,n+1): TMP=arr[i-1]+arr[i-2] Arr.append (TMP)returnArr[n]PrintF2 (8)
View Code2. If the frog can jump 1 levels at a time, you can jump 2 levels at a time, jump 3 levels at once, ..., jump once n-n level. Q. How many ways to jump on the nth step?
#. *. coding:utf-8-*#Recursive Solutiondeff (n):ifN==1:        return1Else:        return2*f (n-1)PrintF (8)#Bottom -up solutiondefF2 (n): Arr=[0,1,2]     forIinchRange (3,n+1): TMP=2*arr[i-1] Arr.append (TMP)returnArr[n]PrintF2 (8)
View Code

Python----Dynamic Planning

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.