[python] Dynamic programming notes [2] Sequential dynamic programming

Sequential dynamic programming

Insert picture description here
Step 1: Determine the state
· The optimal strategy is the strategy with the least cost
· The last step: In the strategy with the least cost, house N-1 must be dyed in one
of the three colors of red, green and blue · but adjacent Two houses cannot be dyed the same color
· If you apply the previous idea, record the minimum cost of the first N houses. According to the routine, you should also record the minimum cost of the first N-1 houses. However, in the first N-1 houses, no Know what color the house N-2 is, so it may collide with the house N-1.
· **So, -> I don’t know what color the house N-2 is, just record it. **Record separately N-1 houses before painting and house N-2 is the minimum cost of red, blue, and green.

Sub-problem: Find the minimum cost of N houses before painting and house N-1 is red, blue, and green.
Status: Suppose i house before painting and house i-1 is red, blue, and green. The minimum costs are f[i][0], f[i][1], f[i][2]
Insert picture description here
Insert picture description here

The solution is as follows:

class Solution:
    def calculate(self, N, cost):
		#red指第N个房子为红的情况,blue、green同理
        red, blue, green = cost[0][0], cost[0][1], cost[0][2]
        for i in range(1, N):
            red,blue,green = min(blue + cost[i][0], green + cost[i][0]),min(red + cost[i][1], green + cost[i][1]), min(red + cost[i][2], blue + cost[i][2])
        return min(red, blue, green)

Summary:
1. Sequential dynamic programming: …first i…minimum/number of ways/feasibility
2. If only f(N-1) is used, it will not be able to distinguish.
3. Solution: record the situation of N-2


Insert picture description here
Determine the status:
· The decrypted number string is divided into several segments, each segment corresponds to a letter
· The last step (the last segment) corresponds to a letter
· This letter becomes 1, 2..., or 26 when encrypted,
Insert picture description here
Insert picture description here
so the decryption method should be 100+50 kinds————

**Get the sub-question ->** Know how to decrypt the first N-1 and first N-2 characters of the number string

Obtain the state equation: suppose the first i digits of the number string S are decrypted into a letter string, there are f[i] ways

Transfer equation
Insert picture description here
Insert picture description here
Insert picture description here

class Solution:
    def calculate(self, str):
        n = len(str)
        f = [0] * (n+1)

        f[0] = 1
        if n == 0:
            return 0
        for i in range(1, n+1):
            #判断最后一位
            t = ord(str[i-1]) - ord('0')
            if t >= 1 and t <= 9:
                f[i] += f[i-1]

            #判断最后两个地址
            if i >= 2:
                t = (ord(str[i-2]) - ord('0')) * 10 + (ord(str[i-1]) - ord('0'))
                if t >= 10 and t <= 26:
                    f[i] += f[i-2]
        return f[n]

Guess you like

Origin blog.csdn.net/Sgmple/article/details/113103552