knaxuslearn · build · ship

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.

  1. 105House Robber (opens LeetCode in a new tab)Take or skipMediumcore
    Common mistake
    Missing the base cases for the first two houses.
  2. 106House Robber II (opens LeetCode in a new tab)Circle: run the line version twiceMedium
    Common mistake
    n == 1 needs a special case.
  3. 107Longest Palindromic Substring (opens LeetCode in a new tab)Expand around the centerMedium
    Common mistake
    Forgetting the even-length center.
  4. 108Decode Ways (opens LeetCode in a new tab)1-D DP on a stringMedium
    Common mistake
    Mishandling the digit '0'.
  5. 109Coin Change (opens LeetCode in a new tab)Unbounded knapsack (minimum)Mediumcore
    Common mistake
    Adding 1 to infinity, and forgetting the unreachable case.
  6. 110Word Break (opens LeetCode in a new tab)DP over prefixesMedium
    Common mistake
    Forgetting dp[0] = true.
  7. 111Longest Increasing Subsequence (opens LeetCode in a new tab)DP with a double loopMediumcore
    Common mistake
    Returning dp[n-1] instead of the max.
  8. 112Maximum Product Subarray (opens LeetCode in a new tab)Track the max and the minMediumcore
    Common mistake
    Overwriting cur_max before computing cur_min.