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.
- 11Valid Palindrome (opens LeetCode in a new tab)Two pointers from both endsEasy
- Common mistake
- Inner skip loops that forget to check l < r.
- 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).
- 133Sum (opens LeetCode in a new tab)Sort + two pointersMedium
- Common mistake
- Placing the duplicate-skipping in the wrong spot.
- 14Sort Colors (opens LeetCode in a new tab)Three pointers (low, mid, high)Medium
- Common mistake
- Advancing mid after a swap with high.
- 15Trapping Rain Water (opens LeetCode in a new tab)Two pointers with left and right maxHard
- Common mistake
- Updating the max after computing water instead of before.
- 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.
- 17Longest Substring Without Repeating Characters (opens LeetCode in a new tab)Sliding window + setMedium
- Common mistake
- Not removing characters from the set as the window shrinks.
- 18Longest Repeating Character Replacement (opens LeetCode in a new tab)Window with a validity ruleMedium
- Common mistake
- Trying to lower max_freq when shrinking. It does not need to go down.
- 19Permutation in String (opens LeetCode in a new tab)Fixed-size window + countsMedium
- Common mistake
- Off-by-one when building the first window.
- 20Minimum Window Substring (opens LeetCode in a new tab)Variable window with need and haveHard
- Common mistake
- Updating the satisfied count at the wrong moment (only when a count reaches its need).