Dynamic programming - cocoder39/coco39_LC GitHub Wiki
How to identify a DP problem:
- optimal solution of a problem is constructed based on optimal solution of subproblems (same property as greedy)
- subproblems overlap: subproblems used to construct one problem may also be used to construct another problem
recursion + memo: Compared with DP, recursion is sometimes straightforward. With DP, you may get opportunity to reduce memory usage
- 688. Knight Probability in Chessboard
- 494. Target Sum
- 416. Partition Equal Subset Sum
- 329. Longest Increasing Path in a Matrix
- 1866. Number of Ways to Rearrange Sticks With K Sticks Visible
- 568. Maximum Vacation Days
- 1359. Count All Valid Pickup and Delivery Options
problem
- min/max value
- valid/invalid
-
valid solutions
core:
-
initial state
-
state transition
-
131. Palindrome Partitioning (backtracking)
-
264. Ugly Number II (263. Ugly Number,23. Merge k Sorted Lists)
-
313. Super Ugly Number (heap sort)
word break
enumeration
- 198. House Robber
- 213. House Robber II
- 337. House Robber III
- 376. Wiggle Subsequence
- 276. Paint Fence
- 256. Paint House
- 265. Paint House II (use input memory)
recursion
- 96. Unique Binary Search Trees (catalan number (follow-ups))
- 95. Unique Binary Search Trees II
- 241. Different Ways to Add Parentheses (not DP, recursion)
- 120. Triangle (recursion + memo or dp)
range sum
-
209. Minimum Size Subarray Sum (two pointers / binary search)
-
523. Continuous Subarray Sum (prefix-sum + variant of 2-sum)
-
560. Subarray Sum Equals K (prefix-sum + variant of 2-sum)
max subarray
- 53. Maximum Subarray (Kadane algorithm)
- 152. Maximum Product Subarray
- 1352. Product of the Last K Numbers
stock
- 121. Best Time to Buy and Sell Stock (53. Maximum Subarray)
- 122. Best Time to Buy and Sell Stock II (greedy)
- 123. Best Time to Buy and Sell Stock III (121. Best Time to Buy and Sell Stock / enumerate state)
- 188. Best Time to Buy and Sell Stock IV (Kadane algorithm / enumerate state)
- 309. Best Time to Buy and Sell Stock with Cooldown (enumerate state with using state machine)
- 714. Best Time to Buy and Sell Stock with Transaction Fee (enumerate state)
longest increasing sequence
- 300. Longest Increasing Subsequence (dp / binary search)
- 334. Increasing Triplet Subsequence
- 354. Russian Doll Envelopes
- Interval Scheduling
- 1626. Best Team With No Conflicts
Misc
- 32. Longest Valid Parentheses (stack invalid index / dp extends gap)
- 361. Bomb Enemy
- 1931. Painting a Grid With Three Different Colors (bit mask + dfs + dp)
2D
string match
- 10. Regular Expression Matching
- 44. Wildcard Matching (rolling array, add two pointers later)
- 72. Edit Distance (rolling array)
- 161. One Edit Distance (two pointers)
- 115. Distinct Subsequences (rolling array)
- 97. Interleaving String (rolling array)
- 5. Longest Palindromic Substring
- 712. Minimum ASCII Delete Sum for Two Strings
- 718. Maximum Length of Repeated Subarray
- 1143. Longest Common Subsequence
- 2060. Check if an Original String Exists Given Two Encoded Strings
matrix
- 62. Unique Paths (rolling array, combination number) (path)
- 63. Unique Paths II (path, rolling array)
- 64. Minimum Path Sum (path, rolling array)
- 174. Dungeon Game (path, rolling array)
- 221. Maximal Square (height)
- 84. Largest Rectangle in Histogram (not dp, monotonically increasing stack)
- 85. Maximal Rectangle (height, 84. Largest Rectangle in Histogram)
- 750. Number Of Corner Rectangles
1d to 2d
3D
string
- 87. Scramble String (recursion)
Minimax
Knapsack Problem
- 0-1 Knapsack Problem
- Bounded Knapsack Problem
- Unbounded Knapsack problem
Jump Game (dp or greedy kinda of like BFS)
- 45. Jump Game II
- 1326. Minimum Number of Taps to Open to Water a Garden
- 1024. Video Stitching
- 1871. Jump Game VII
左右双向dp