knaxuslearn · build · ship

week 2 of 15

Two pointers and sliding window

Get comfortable with these coding mechanics first: two index variables, while-loop conditions, a window defined by left and right, shrinking a window inside a loop. Template to type from memory by the end of the week: sliding window: for each right end, add it, then while the window is invalid, shrink from the left, then record the answer.

  1. 11Valid Palindrome (opens LeetCode in a new tab)Two pointers from both endsEasy
    Common mistake
    Inner skip loops that forget to check l < r.
  2. 12Container With Most Water (opens LeetCode in a new tab)Two pointers, move the shorter sideMedium
    Common mistake
    Moving the taller pointer (it can never help).
  3. 133Sum (opens LeetCode in a new tab)Sort + two pointersMediumcore
    Common mistake
    Placing the duplicate-skipping in the wrong spot.
  4. 14Sort Colors (opens LeetCode in a new tab)Three pointers (low, mid, high)Medium
    Common mistake
    Advancing mid after a swap with high.
  5. 15Trapping Rain Water (opens LeetCode in a new tab)Two pointers with left and right maxHardcore
    Common mistake
    Updating the max after computing water instead of before.
  6. 16Rotate Image (opens LeetCode in a new tab)In-place matrix index mathMedium
    Common mistake
    Looping j over the whole row in the transpose, which undoes the swaps.
  7. 17Longest Substring Without Repeating Characters (opens LeetCode in a new tab)Sliding window + setMediumcore
    Common mistake
    Not removing characters from the set as the window shrinks.
  8. 18Longest Repeating Character Replacement (opens LeetCode in a new tab)Window with a validity ruleMediumcore
    Common mistake
    Trying to lower max_freq when shrinking. It does not need to go down.
  9. 19Permutation in String (opens LeetCode in a new tab)Fixed-size window + countsMedium
    Common mistake
    Off-by-one when building the first window.
  10. 20Minimum Window Substring (opens LeetCode in a new tab)Variable window with need and haveHardcore
    Common mistake
    Updating the satisfied count at the wrong moment (only when a count reaches its need).