week 14 of 15
Dynamic programming II: tables and knapsack
Get comfortable with these coding mechanics first: a 2-D table sized (rows+1) by (cols+1), the order to fill it, base row and column. Template to type from memory by the end of the week: draw the table for a tiny example first, fill it by hand, then write the loops.
- 113Unique Paths (opens LeetCode in a new tab)Grid DPMedium
- Common mistake
- Mixing up rows and columns.
- 114Longest Common Subsequence (opens LeetCode in a new tab)2-D DP on two stringsMedium
- Common mistake
- Getting the -1 offset wrong when reading characters.
- 115Edit Distance (opens LeetCode in a new tab)2-D DP on two stringsMedium
- Common mistake
- Missing the base cases (an empty string on either side).
- 116Partition Equal Subset Sum (opens LeetCode in a new tab)0/1 knapsackMedium
- Common mistake
- Looping sums upward reuses an item twice.
- 117Coin Change II (opens LeetCode in a new tab)Unbounded knapsack (count)Medium
- Common mistake
- Coins outer counts combinations. Amount outer counts orderings.
- 118Target Sum (opens LeetCode in a new tab)Subset-sum countMedium
- Common mistake
- Skipping the parity and negative checks.
- 119Best Time to Buy and Sell Stock with Cooldown (opens LeetCode in a new tab)State machine DPMedium
- Common mistake
- Updating one state and then using the new value for the next.
- 120Burst Balloons (opens LeetCode in a new tab)Interval DP (hard)Hard
- Common mistake
- Treating k as the first balloon to burst. Treat it as the last one in the range.