Dynamic programming template-Python version

This article is from https://leetcode.com/discuss/general-discussion/458695/Dynamic-Programming-Patterns,  this foreign netizen, I am  responsible for the localization

table of Contents

1. The minimum (maximum) path-type problem to reach the goal

Two, different ways

3. Consolidation interval

Four, string DP problem

Fourth, make decision-making problems


1. The minimum (maximum) path-type problem to reach the goal

The description of this type of problem is: Given a goal, find the minimum (maximum) cost (cost)/path (path)/sum (sum) to reach the goal.

The general template code is:

for i in range(1,target+1):
    for j in range(len(ways)):# ways:达到目标的方法个数
        if ways[i]<=i:
            dp[i] = min(dp[i], dp[i - ways[j]] + 代价(cost) / 路径(path) / 总和(sum))
return dp[target]

Similar questions are:

  1. Climb stairs with minimal cost
  2. Minimum path sum
  3. Change exchange
  4. Minimum sum of descent path
  5. Lowest fare
  6. Keyboard with only two keys

  7. Perfect square number

  8. Last Stone Weight II 

  9. Triangle 

  10. Ones and Zeroes 

  11. Maximal Square

  12. Coin Change

  13. Tiling a Rectangle with the Fewest Squares

  14. Dungeon Game

  15. Minimum Number of Refueling Stops 

Two, different ways

The description of this type of problem is: given a goal, find different ways to reach the goal

The general template code is:

for i in range(1,target):
    for j in range(len(ways)):
        if ways[j]<=i:
            dp += dp[i-ways[j]]
return dp[target] 

Similar questions are:

  1. Climbing Stairs 
  2. Unique Paths 
  3. Number of Dice Rolls With Target Sum

There are also questions that indicate the number of repetitions. In this case, add a loop to simulate each repetition.

  1. Knight Probability in Chessboard 
  2. Target Sum 
  3. Combination Sum IV 
  4. Knight Dialer 
  5. Dice Roll Simulation 
  6. Partition Equal Subset Sum 
  7. Soup Servings 
  8. Domino and Tromino Tiling
  9. Minimum Swaps To Make Sequences Increasing
  10. Number of Longest Increasing Subsequence 
  11. Unique Paths II
  12. Out of Boundary Paths 
  13. Number of Ways to Stay in the Same Place After Some Steps 
  14. Count Vowels Permutation 

3. Consolidation interval

The description of this kind of problem is: given a set of numbers to get the best value, you can find the solution to the problem from the current number and the number on the left or right of it.

The general template code is:

for l in range(1,n):
    for i in range(n-l):
        j = i+l
        for k in range(i,j):
            dp[i][j] = max(dp[i][j], dp[i][k] + result[k] + dp[k+1][j])
return dp[0][n-1]

Similar questions are:

  1. Minimum Cost Tree From Leaf Values
  2. Unique Binary Search Trees
  3. Minimum Score Triangulation of Polygon 
  4. Remove Boxes 
  5. Minimum Cost to Merge Stones 
  6. Burst Balloons 
  7. Guess Number Higher or Lower II

Four, string DP problem

The description of this type of problem is: Given two strings s1 and s2, return some results

The general template code is:

/**
i : 字符串s1的下标
j : 字符串s2的下标
**/
for i in range(1,n+1):
    for j in range(1,m+1):
        if s1[i-1] == s2[j-1]:
            dp[i][j] = #code....#
        else:
            dp[i][j] = #code....#

If you give yourself a string, the method is almost the same:

for l in range(1,n):
    for i in range(n-l):
        j = i+l
        if s[i] == s[j]:
            dp[i][j] = /*code*/
        else:
            dp[i][j] = /*code*/

Similar questions are:

  1. Longest Common Subsequence 
  2. Palindromic Substrings
  3.  Longest Palindromic Subsequence 
  4. Shortest Common Supersequence 
  5. Edit Distance
  6. Distinct Subsequences 
  7. Minimum ASCII Delete Sum for Two Strings
  8. Longest Palindromic Substring

Fourth, make decision-making problems

Problem description: Given a set of values, find the answer, and provide the option to choose or ignore the current value

Generic template code:

#i: 一组值的下标
#j: 忽略j值的选项
for i in range(1,n):
    for j in range(1,k+1):
        dp[i][j] = max(dp[i][j], dp[i-1][j] + arr[i], dp[i-1][j-1])
        dp[i][j-1] = max(dp[i][j-1], dp[i-1][j-1] + arr[i], arr[i])
        

Similar questions are:

  1. House Robber 
  2. Best Time to Buy and Sell Stock
  3. Best Time to Buy and Sell Stock with Transaction Fee
  4. Best Time to Buy and Sell Stock with Cooldown 
  5. Best Time to Buy and Sell Stock III 
  6. Best Time to Buy and Sell Stock IV 

In fact, many problems can be solved without dynamic programming. Here is just a summary of the solutions to similar problems.

Guess you like

Origin blog.csdn.net/Matrix_cc/article/details/109584049