TodayILearned
Algorithms lessons
15 of 53 published lessons in this track - taught deeply, then shared publicly.
15 entries
Trapping Rain Water - water above every index
Per-index min(leftMax,rightMax)-height. Two pointers O(1) space. 2764x vs brute, 15.7x vs precompute at n=2K.
Container With Most Water - move the shorter wall
Converging two pointers maximize min(height)×width. Always advance the shorter side. 324.6x vs brute at n=2K.
3Sum - fix one index, then run Two Sum II on the rest
Sort once, fix i, Day 40 squeeze for -nums[i], skip duplicates. O(n2). 236.9x vs brute at n=300.
Two Sum II - sorted array, squeeze from both ends
Day 11 needed a hash map. Sorted input flips the tool: converging two pointers - O(n) time, O(1) space. 13.0x vs binary, 59.4x vs hash at n=100K.
Valid Palindrome - two pointers that walk toward each other
Day 33 moved pointers in the same direction. Today they converge from both ends, skipping noise inline. No-alloc two-ptr: 6.3× faster than reverse at n=100K.
Is Subsequence - Two Pointers, Order Kept
Two pointers: walk t once, advance i only on match. Subsequence ≠ substring. Lab: indexOf ~97× vs two-pointer JS on |t|=500K.
Valid Sudoku - One Pass, Three Memberships
Row / col / box Sets in one pass. Box index floor(r/3)*3+floor(c/3). Lab: bitmask 2.99× vs brute; Sets slower (alloc tax) on 20K boards.
Longest consecutive sequence - hash-set sequence heads
Only expand from sequence heads (n-1 missing). Amortized O(n) with a Set - 124.7× faster than brute at n=5K mixed.
Encode / Decode Strings - Length-Prefix Framing
join("#") is not a codec. Prefix each string with its length, slice by that length on decode - 14.3× faster than escaping on hostile data.
Product of array except self - prefix × suffix
The no-division constraint forces a reframe: the answer for each position is the product of everything before it × everything after it. Two passes, O(n) time, O(1) auxiliary space.
Top K Frequent Elements - Heap vs Bucket Sort
Day 5 counted frequencies. Day 12 grouped anagrams by a canonical signature. Today: find the top K. Three approaches, three Big O complexities - and a benchmark that shows the textbook O(n) answer isn't always the fastest in practice.
Group anagrams - sort key vs count key
Two ways to build a canonical key: sort each string (O(n × k log k)) or count characters (O(n × k)). The benchmark reveals a surprising crossover.
Two Sum - hash map complement lookup
The #1 most-asked LeetCode problem. One pass, one hash map, one key insight: for each number, check if you've already seen its complement.
Valid Anagram - the frequency count pattern
When the question is 'how many times?', a hash map is the answer.
Contains Duplicate - the hash set pattern
When 'have I seen this before?' is the question, a hash set is the answer.