Binary Search Framework: One Template for Element Search and Left/Right Boundaries
Unify "search for an element," "search for the left boundary," and "search for the right boundary" into a single half-open interval skeleton [left, right): how to fill in the three blanks (condition, shrink direction, return value), why lowerBound and upperBound differ by just one comparison operator, why the right boundary is upperBound - 1, and three troubleshooting checklists for mid overflow, infinite loops, and out-of-bounds return values. Includes implementations in C, C++, and JavaScript.
4433 words
|
22 minutes
Difference Arrays in Practice: One Template for Range Addition, Flight Bookings, and Car Pooling, Plus Four Extensions
LeetCode 370 Range Addition, 1109 Corporate Flight Bookings, and 1094 Car Pooling all reduce to the same "mark both endpoints + prefix-sum restore" template: how to write it with an extra slot, how to convert 1-indexed input, and why the end is right-open, with implementations in C, C++, and JavaScript. The post then extends to 2D difference arrays, tree difference, sweep-line thinking, and structure selection.
4500 words
|
23 minutes
Prefix Sums in Practice: Range Queries on Immutable Arrays and Immutable Matrices, and the Problems They Can't Solve
Converge LeetCode 303 and 304, both "Immutable" problems, onto a single "one extra slot" template: how 1D pre[r+1] - pre[l] and the 2D inclusion-exclusion four corners work, with implementations in C, C++, and JavaScript. Then look at four kinds of problems prefix sums can't handle (dynamic updates, operations without inverses, overflow, and lost internal structure) and what structures to use instead.
3702 words
|
19 minutes
Classic Sliding Window Problems (Part 2): Changing State, Reverse Transformation, and Enumerating Dimensions
A deep dive into four advanced sliding window problems: Contains Duplicate II, Contains Duplicate III, Minimum Operations to Reduce X to Zero, and Longest Substring with At Least K Repeating Characters. Each demonstrates a different variation—swapping window state for hash maps and bucketing, reversing the problem, and enumerating dimensions—with C / C++ / JavaScript implementations.
7663 words
|
38 minutes
Classic Sliding Window Algorithm Problems (Part 1): Sum, Product, and Counting Within a Window
A detailed walkthrough of four variable-length window problems: Minimum Size Subarray Sum, Subarray Product Less Than K, Max Consecutive Ones III, and Longest Repeating Character Replacement. Each problem includes answers to three key questions, step-by-step diagrams, common pitfalls, and implementations in C, C++, and JavaScript.
5336 words
|
27 minutes
Sliding Window Core Template: Three Questions to Solve Any Substring or Subarray Problem
Distill a unified sliding window template from four classic problems—minimum window substring, permutation in string, find all anagrams in a string, and longest substring without repeating characters—by answering three questions: when to expand, when to shrink, and when to update the result. Also explains why the window interval is best written as left-closed, right-open. Includes implementations in C, C++, and JavaScript.
6468 words
|
32 minutes
Classic Two-Pointer Array Problems (Part 2): Matrices and Strings
A detailed walkthrough of the last four problems in LeetCode's "Array and Two Pointers" introductory plan: sorting a matrix diagonally, shifting a 2D grid, transposing a matrix, and finding the longest common prefix. Covers techniques such as identifying diagonals with i - j, flattening with index mapping, triple reversal, and vertical scanning, with implementations in C, C++, and JavaScript.
4755 words
|
24 minutes
Classic Two-Pointer Array Problems (Part 1): One-Dimensional Arrays and Strings
A walkthrough of the first five problems in LeetCode's "Array and Two Pointers" introductory plan: removing duplicates from a sorted array, validating a palindrome, sorting colors, merging two sorted arrays, and squaring a sorted array. Each problem includes pointer invariants, step-by-step diagrams, common pitfalls, and implementations in C, C++, and JavaScript.
4812 words
|
24 minutes