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
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:
- Climb stairs with minimal cost
- Minimum path sum
- Change exchange
- Minimum sum of descent path
- Lowest fare
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:
There are also questions that indicate the number of repetitions. In this case, add a loop to simulate each repetition.
- Knight Probability in Chessboard
- Target Sum
- Combination Sum IV
- Knight Dialer
- Dice Roll Simulation
- Partition Equal Subset Sum
- Soup Servings
- Domino and Tromino Tiling
- Minimum Swaps To Make Sequences Increasing
- Number of Longest Increasing Subsequence
- Unique Paths II
- Out of Boundary Paths
- Number of Ways to Stay in the Same Place After Some Steps
- 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:
- Minimum Cost Tree From Leaf Values
- Unique Binary Search Trees
- Minimum Score Triangulation of Polygon
- Remove Boxes
- Minimum Cost to Merge Stones
- Burst Balloons
- 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:
- Longest Common Subsequence
- Palindromic Substrings
- Longest Palindromic Subsequence
- Shortest Common Supersequence
- Edit Distance
- Distinct Subsequences
- Minimum ASCII Delete Sum for Two Strings
- 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:
- House Robber
- Best Time to Buy and Sell Stock
- Best Time to Buy and Sell Stock with Transaction Fee
- Best Time to Buy and Sell Stock with Cooldown
- Best Time to Buy and Sell Stock III
- 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.