Hello everyone, I am a Chinese noob programmer. I have practiced questions on leetcode.com for 2 years. During this time, I studied a lot from many Great Gods’ articles. After worship, I always wanted to write an article as they did, and now I take the courage to write my first article here.
This is an article summarizing topics related to dynamic programming. Its main inspiration comes from these two articles: Dynamic Programming Patterns and 怎樣學好動態規劃? - 窮碼農的回答 - 知乎. DP is always a very difficult problem for me and lately, I have done dozens of questions to improve my DP skills. But the DP problem is so flexible that random practice has little effect. Therefore, I made further improvements based on these two articles and summarize some typical topics of DP problem. I hope this article can help you.
In this article, all problems come from leetcode.com and leetcode-cn.com which is a Chinese version of leetcode.com. By the way, because I am Chinese, I attached the Chinese translation after the English topics so that I can use this article more convenient in the future. Moreover, can anyone tell me how to create a table of contents? I wrote this article with VSCode and attempted many extensions to do this but all of them failed. I will be very grateful if you can tell me a way to achieve this function.
I will continue practicing DP problems on leetcode.com and will keep updating this article. If you have any better suggestions or supplements, welcome to put them in the comment area and @me. Now the main text begins.
1. Number Tower,數塔
- 118. Pascal’s Triangle
(Easy)
、118. 楊輝三角(簡單)
- 119. Pascal’s Triangle II
(Easy)
、119. 楊輝三角 II(簡單)
- 120. Triangle
(Medium)
、120. 三角形最小路徑和(中等)
- 64. Minimum Path Sum
(Medium)
、64. 最小路徑和(中等)
- 931. Minimum Falling Path Sum
(Medium)
、931. 下降路徑最小和(中等)
2. Fibonacci Numbers,斐波那契數列
- Fibonacci numbers,斐波那契數列問題
- 509. Fibonacci Number
(Easy)
、509. 斐波那契數(簡單)
- 1137. N-th Tribonacci Number
(Easy)
、1137. 第 N 個泰波那契數(簡單)
- 509. Fibonacci Number
- Staircase,爬樓梯問題
- 70. Climbing Stairs
(Easy)
、70. 爬樓梯(簡單)
- 746. Min Cost Climbing Stairs
(Easy)
、746. 使用最小花費爬樓梯(簡單)
- 70. Climbing Stairs
- House thief,偷房子問題
- 198. House Robber
(Easy)
、198. 打家劫舍(簡單)
- 198. House Robber
3. 0/1 Knapsack, 0/1 揹包
- Equal Subset Sum Partition,相等子集劃分問題
- 416. Partition Equal Subset Sum
(Medium)
、416. 分割等和子集(中等)
- 416. Partition Equal Subset Sum
- Subset Sum,子集和問題
- 494. Target Sum
(Medium)
、494. 目標和(中等)
- 494. Target Sum
- Minimum Subset Sum Difference,子集和的最小差問題
- 1049. Last Stone Weight II
(Medium)
、1049. 最後一塊石頭的重量 II(中等)
- 1049. Last Stone Weight II
- Other,其它
- 474. Ones and Zeroes
(Medium)
、474. 一和零(中等)
- 474. Ones and Zeroes
4. Unbounded Knapsack,無限(完全)揹包
- Coin Change,換硬幣問題
- 322. Coin Change
(Medium)
、322. 零錢兌換(中等)
- 518. Coin Change 2
(Medium)
、518. 零錢兌換 II(中等)
- 322. Coin Change
- Others,其它
5. Counting DP,計數 DP
5.1. Total Number of Solutions,方案總數
- 62. Unique Paths
(Medium)
、62. 不同路徑(中等)
- 63. Unique Paths II
(Medium)
、63. 不同路徑 II(中等)
- 1155. Number of Dice Rolls With Target Sum
(Medium)
、1155. 擲骰子的N種方法(中等)
- 1223. Dice Roll Simulation
(Medium)
、1223. 擲骰子模擬(中等)
- 935. Knight Dialer
(Medium)
、935. 騎士撥號器(中等)
- 576. Out of Boundary Paths
(Medium)
、576. 出界的路徑數(中等)
- 790. Domino and Tromino Tiling
(Medium)
、790. 多米諾和托米諾平鋪(中等)
- 91. Decode Ways
(Medium)
、91. 解碼方法(中等)
5.2. Probability,概率
- 688. Knight Probability in Chessboard
(Medium)
、688-cn. “馬”在棋盤上的概率(中等)
- 808. Soup Servings
(Medium)
、808. 分湯(中等)
6. Decision Making,決策問題
6.1. Buy and Sell Stock,買賣股票問題
- 309. Best Time to Buy and Sell Stock with Cooldown
(Medium)
、309. 最佳買賣股票時機含冷凍期(中等)
- 714. Best Time to Buy and Sell Stock with Transaction Fee
(Medium)
、714. 買賣股票的最佳時機含手續費(中等)
- 123. Best Time to Buy and Sell Stock III
(Hard)
、123. 買賣股票的最佳時機 III(困難)
- 188. Best Time to Buy and Sell Stock IV
(Hard)
、188. 買賣股票的最佳時機 IV(困難)
6.2. Can I Win? 遊戲勝負問題
- 464. Can I Win
(Medium)
、464. 我能贏嗎(中等)
- 877. Stone Game
(Medium)
、877. 石子游戲(中等)
- 1140. Stone Game II
(Medium)
、1140. 石子游戲 II(中等)
- 1406. Stone Game III
(Hard)
、1406. 石子游戲 III(困難)
6.3. Minimum Cost,最少花費問題
- 983. Minimum Cost For Tickets
(Medium)
、983. 最低票價(中等)
- 650. 2 Keys Keyboard
(Medium)
、650. 只有兩個鍵的鍵盤(中等)
- 279. Perfect Squares
(Medium)
、279. 完全平方數(中等)
- 801. Minimum Swaps To Make Sequences Increasing
(Medium)
、801. 使序列遞增的最小交換次數(中等)
- 256. Paint House
(Easy)
、256. 粉刷房子(簡單)
- 265. Paint House II
(Hard)
、265. 粉刷房子 II(困難)
- 1473. Paint House III
(Hard)
、1473. 給房子塗色 III(困難)
7. Interval DP,區間 DP
- Intervals Merge,區間合併
- 1130. Minimum Cost Tree From Leaf Values
(Medium)
、1130. 葉值的最小代價生成樹(中等)
- 96. Unique Binary Search Trees
(Medium)
、96. 不同的二叉搜索樹(中等)
- 1130. Minimum Cost Tree From Leaf Values
- Triangulation,三角剖分
- Rectangle Segmentation,矩形分割
- 221. Maximal Square
(Medium)
、221. 最大正方形(中等)
- 1277. Count Square Submatrices with All Ones
(Medium)
、1277. 統計全爲 1 的正方形子矩陣(中等)
- 85. Maximal Rectangle
(Hard)
、85. 最大矩形(困難)
- 221. Maximal Square
- Others,其它
- 1478. Allocate Mailboxes
(Hard)
、1478. 安排郵筒(困難)
- 1478. Allocate Mailboxes
8. Subsequence/Substring,子序列/子字符串
8.1. Longest Subsequence/Substring,最長子序列/子字符串
- Longest Common Substring,最長公共子串
- 718. Maximum Length of Repeated Subarray
(Medium)
、718. 最長重複子數組(中等)
- 718. Maximum Length of Repeated Subarray
- Longest Common Subsequence,最長公共子序列
- 1143. Longest Common Subsequence
(Medium)
、1143. 最長公共子序列(中等)
- 1035. Uncrossed Lines
(Medium)
、1035. 不相交的線(中等)
- 1458. Max Dot Product of Two Subsequences
(Hard)
、1458. 兩個子序列的最大點積(困難)
- 1143. Longest Common Subsequence
- Longest Increasing Subsequence,最長上升子序列
- Shortest Common Super-sequence,最短公共超級子序列
- Others,其它
- 368. Largest Divisible Subset
(Medium)
、368. 最大整除子集(中等)
- 368. Largest Divisible Subset
8.2. Palindromic Subsequence/Substring,迴文子序列/子字符串
- Longest Palindromic Subsequence,最長迴文子序列
- 516. Longest Palindromic Subsequence
(Medium)
、516. 最長迴文子序列(中等)
- 516. Longest Palindromic Subsequence
- Longest Palindromic Substring,最長迴文子字符串
- 5. Longest Palindromic Substring
(Medium)
、5. 最長迴文子串(中等)
- 5. Longest Palindromic Substring
- Count Palindromic Subsequences/Substrings,迴文子序列/子字符串的個數
- 647. Palindromic Substrings
(Medium)
、647. 迴文子串(中等)
- 730. Count Different Palindromic Subsequences
(Hard)
、730. 統計不同迴文子字符串(困難)
- 647. Palindromic Substrings
- Palindromic Partitioning,迴文分割
- 131. Palindrome Partitioning
(Medium)
、131. 分割回文串(中等)
- 132. Palindrome Partitioning II
(Hard)
、132. 分割回文串 II(困難)
- 131. Palindrome Partitioning
9. String,字符串上的動態規劃
9.1. String Transform,字符串變換
- 583. Delete Operation for Two Strings
(Medium)
、583. 兩個字符串的刪除操作(中等)
- 712. Minimum ASCII Delete Sum for Two Strings
(Medium)
、712. 兩個字符串的最小ASCII刪除和(中等)
- 72. Edit Distance
(Hard)
、72. 編輯距離(困難)
9.2. Regular Expression Matching,正則表達式匹配
- 678. Valid Parenthesis String
(Medium)
、678. 有效的括號字符串(中等)
- 10. Regular Expression Matching
(Hard)
、10. 正則表達式匹配(困難)
- 44. Wildcard Matching
(Hard)
、44. 通配符匹配(困難)
- 639. Decode Ways II
(Hard)
、639. 解碼方法 2(困難)
10. Multi-start state,多起始狀態
- 741. Cherry Pickup
(Hard)
、741. 摘櫻桃(困難)
- 1463. Cherry Pickup II
(Hard)
、1463. 摘櫻桃 II(困難)
11. DP Optimizition,DP優化
- 1434. Number of Ways to Wear Different Hats to Each Other
(Hard)
、1434. 每個人戴不同帽子的方案數(困難)
- 1425. Constrained Subset Sum
(Hard)
、1425. 帶限制的子序列和(困難)
- 1444. Number of Ways of Cutting a Pizza
(Hard)
、1444. 切披薩的方案數(困難)
- 837. New 21 Game
(Medium)
、837. 新21點(中等)
Thanks for your browsing and best wishes to you.