week 13 of 15
Dynamic programming I: one dimension
Get comfortable with these coding mechanics first: a memo map or a 1-D array, base cases, writing 'dp[i] means ...' in a comment before coding. Template to type from memory by the end of the week: four lines first: what dp[i] means, base cases, the transition, which cell is the answer.
- 105House Robber (opens LeetCode in a new tab)Take or skipMedium
- Common mistake
- Missing the base cases for the first two houses.
- 106House Robber II (opens LeetCode in a new tab)Circle: run the line version twiceMedium
- Common mistake
- n == 1 needs a special case.
- 107Longest Palindromic Substring (opens LeetCode in a new tab)Expand around the centerMedium
- Common mistake
- Forgetting the even-length center.
- 108Decode Ways (opens LeetCode in a new tab)1-D DP on a stringMedium
- Common mistake
- Mishandling the digit '0'.
- 109Coin Change (opens LeetCode in a new tab)Unbounded knapsack (minimum)Medium
- Common mistake
- Adding 1 to infinity, and forgetting the unreachable case.
- 110Word Break (opens LeetCode in a new tab)DP over prefixesMedium
- Common mistake
- Forgetting dp[0] = true.
- 111Longest Increasing Subsequence (opens LeetCode in a new tab)DP with a double loopMedium
- Common mistake
- Returning dp[n-1] instead of the max.
- 112Maximum Product Subarray (opens LeetCode in a new tab)Track the max and the minMedium
- Common mistake
- Overwriting cur_max before computing cur_min.