# Dynamic Programming algorithm

Source: Internet
Author: User
Tags for in range

1. Maximum continuous product sub-array

Given a floating-point array, take any number of successive numbers in the array to multiply, and find the sub-array with the largest product.

Brute Force Poll:

`def max_substring (list_a, length):     = List_a[0]    for in  Range (length):        = 1 for in         range (i, length):             *= List_a[i]            if x > max_result:                = x    return Max_result`

Time Complexity of O (n^2)

Dynamic planning:

There may be positive, negative, or 0 in the product sub-array.

Due to the existence of negative numbers, consider simultaneously finding the maximum product and the minimum product.

Assuming that the array is a[], the direct use of dynamic programming to solve.

State transition equation:

Maxend = Max (max (Maxend*a[i], minend*a[i]), A[i])

minend = min (max (maxend*a[i], minend*a[i]), A[i])

State equation Initial state:

Maxend = Minend = A[0]

`def max_substring (list_a, length):     = Min_end = list_a[0]    for in range (1, length):        = max_end*List_a[i]         = min_end*list_a[i]        = max (max (End1, End2), List_a[i])        = min (min ( End1, End2), List_a[i])    = max (list_a[0], max_end)    return`

The time complexity is O (n).

2. String editing distance

Given a source string and a target string, you can do the following with the source string:

Insert one character at any location

Replace any character

Delete any character

Write a program that returns the minimum number of operations, so that these operations on the source string are equal to the target string. This is the string editing distance problem.

The problem seems easy to understand.

But the parsing looks very complex and skips over for the time being.

3. Check number problem

There are n*n, each lattice has a positive number or 0, from the top left corner to the bottom right corner, each can only go down or to the right, a walk two times,

Add up all the numbers that pass through the lattice, and find the maximum value of the sum.

If two passes through the same lattice, then the number in the last calculated sum is only added once.

The analysis of the problem is generally understood, the specific code will be added later.

4. Alternating strings

Enter three strings S1 S2 S3 to determine whether the third string is interleaved by the first two strings and does not change the relative order of the characters in each of the two strings.

I thought about the stack method myself. S1 S2 is entered into the stack, and the elements in the S3 are compared to the top elements of the stack respectively. Returns False if the element is the same, or if it is different from the top two elements of the stack.

You can also use a dynamic planning approach. Looks more complex, but can help with understanding dynamic planning.

`classStack (object):def __init__(self): Self.items= []    defIs_empty (self):returnSelf.items = = []    defPeek (self):returnSelf.items[len (Self.items)-1]    defsize (self):returnLen (self.items)defpush (self, item): Self.items.append (item)defpop (self): Self.items.pop ()defIs_interleave (str1, STR2, STR3):ifLen (str1) + len (str2)! =Len (STR3):returnFalse Stack1=Stack () Stack2=Stack () forIinchStr1[::-1]: Stack1.push (i) forIinchStr2[::-1]: Stack2.push (i) forIinchSTR3:ifi = =Stack1.peek (): Stack1.pop ()elifi = =Stack2.peek (): Stack2.pop ()Else:            returnFalsereturnTrue`

This code does not implement the desired functionality. Because the code first tries to match the characters in the str1, and then matches the characters in the str2.

When a character is equal to the top two elements of a stack, it is not possible to determine which string the character should match.

Even if the rules for alternating strings are matched, the characters in str1 all match after the error is exceeded.

Flutter Street.

Dynamic Planning methods:

`defis_interleave (S1, S2, S3): N=Len (S1) m=len (S2) s=Len (S3)ifn + M! =S:returnFalse DP= [None forIinchRange (m+1)] forIinchRange (n+1)] [dp[0][0]=True forIinchRange (n+1):         forJinchRange (m+1):            ifDP[I][J]or(i-1 >= 0 andDP[I-1][J] = = True andS1[i-1] = = S3[i+j-1])or(j-1 >= 0 andDp[i][j-1] = = True andS2[j-1] = = S3[i+j-1]): Dp[i][j]=TrueElse: Dp[i][j]=FalsereturnDP[N][M]`

Dynamic planning perfectly solves the problem. Very powerful.

Note If the judgment condition, the essence of the essence.

At the same time mastered the method of creating the matrix, using the list generator.

Dynamic Programming algorithm

Related Keywords:

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.