Dynamic Programming

Question List

One thing that helped me was coming up with a recursive solution to the problem, then take a look at the parameters you are passing to the recursive function. Then check which parameters change between calls, those are likely the coordinate in your dp array/matrix.

Understand how the recursive call generates its answer from the sub recursive calls, try to put that into an equation, like currentRes = f(subRes). That would be your DP build up equation.

Lastly find the exit case of the recursive function, that is the base case that you need to built up from in DP.

Hope this helps.

Note

# 自顶向下递归的动态规划
def dp(状态1, 状态2, ...):
    for 选择 in 所有可能的选择:
        # 此时的状态已经因为做了选择而改变
        result = 求最值(result, dp(状态1, 状态2, ...))
    return result

# 自底向上迭代的动态规划
# 初始化 base case
dp[0][0][...] = base case
# 进行状态转移
for 状态1 in 状态1的所有取值:
    for 状态2 in 状态2的所有取值:
        for ...
            dp[状态1][状态2][...] = 求最值(选择1,选择2...)

Question types

1D DP

Longest Increasing Subsequence

2D DP

Knapsack questions

Interval DP

877. Stone Game

File confidence date difficulty note
72. Edit Distance 🟡 March 26, 2024 medium -
312. Burst Balloons 🔴 March 26, 2024 hard -
309. Best Time to Buy and Sell Stock with Cooldown 🟡 March 26, 2024 medium -
115. Distinct Subsequences 🔴 March 26, 2024 hard Number of match to exactly first i number of t with subsequence from the first j number of s
10. Regular Expression Matching 🔴 March 26, 2024 hard -
714. Best Time to Buy and Sell Stock with Transaction Fee 🔴 May 10, 2024 medium Buying and selling state indicates the previous action where either buying or selling. Return last of selling
639. Decode Ways II 🟡 June 28, 2024 hard Bottom up DP, consider cases in term of single, double digit, then where the digit is 0, 1 ,2, * or others
32. Longest Valid Parentheses 🔴 June 28, 2024 Hard Subproblem is longest sub sequence give the current index is last digit of the sequence
264. Ugly Number II 🟡 June 28, 2024 medium Can to constant memory, keep one pointer for each of the prime number
940. Distinct Subsequences II 🔴 June 30, 2024 medium Dynamic array, consider in terms of alphabet to remove the repetition
516. Longest Palindromic Subsequence 🔴 June 30, 2024 medium Sub problem is longest palindromic subsequence between left and right. Then consider the case of whether char at left and right are equal or not
1062. Longest Repeating Substring 🔴 September 02, 2024 medium Find longest common substring where the start index is different
276. Paint Fence 🔴 September 19, 2024 medium -
1335. Minimum Difficulty of a Job Schedule 🔴 September 21, 2024 medium Use three parameter to do recursion
473. Matchsticks to Square 🔴 September 25, 2024 medium Sort the array is reversed order to save some time, but regardlessly, it is not a polynomial answer
877. Stone Game 🔴 October 19, 2024 medium -
646. Maximum Length of Pair Chain 🔴 October 19, 2024 medium Longest increasing subsequence dp, or sort by end points and take greedy approach
338. Counting Bits 🟡 October 19, 2024 easy -
300. Longest Increasing Subsequence 🟡 October 19, 2024 medium -
152. Maximum Product Subarray 🟡 October 19, 2024 medium -
1395. Count Number of Teams 🔴 November 11, 2024 medium DP + memo
1014. Best Sightseeing Pair 🔴 November 11, 2024 medium dp[i] mean the largest value it could get from other index before i
97. Interleaving String 🔴 December 13, 2024 medium -
1048. Longest String Chain 🔴 December 13, 2024 - -
518. Coin Change II 🔴 December 22, 2024 medium -
1746. Maximum Subarray Sum After One Operation 🔴 December 29, 2024 medium Kadane's algorithm, with additional twist
2638. Count the Number of K-Free Subsets 🔴 January 11, 2025 medium -

1062. Longest Repeating Substring