brain

tamnd's digital brain — notes, problems, research

43815 notes

LeetCode 452: Minimum Number of Arrows to Burst Balloons

A clear explanation of the greedy interval solution for finding the minimum number of arrows needed to burst all balloons.

leetcodearraygreedysortingintervals
LeetCode 427: Construct Quad Tree

Build a quad tree from a binary square grid using recursive divide and conquer.

leetcodematrixtreequad-treedivide-and-conquerrecursion
LeetCode 402: Remove K Digits

A clear explanation of the Remove K Digits problem using a greedy monotonic stack.

leetcodestackgreedymonotonic-stackstring
LeetCode 377: Combination Sum IV

A clear explanation of Combination Sum IV using dynamic programming to count ordered combinations that sum to a target.

leetcodearraydynamic-programmingunbounded-knapsack
LeetCode 352: Data Stream as Disjoint Intervals

A clear explanation of maintaining disjoint sorted intervals from a stream using insertion and merging.

leetcodedesignintervalsbinary-searchdata-stream
LeetCode 327: Count of Range Sum

A clear explanation of Count of Range Sum using prefix sums and merge sort counting.

leetcodearrayprefix-summerge-sortdivide-and-conquer
LeetCode 302: Smallest Rectangle Enclosing Black Pixels

A clear explanation of Smallest Rectangle Enclosing Black Pixels using binary search on rows and columns.

leetcodematrixbinary-searcharray
LeetCode 277: Find the Celebrity

A two-pass solution for finding a celebrity using the knows API with O(n) calls and O(1) extra space.

leetcodegraphtwo-pointersinteractive
LeetCode 202: Happy Number

A clear explanation of detecting whether repeated digit-square sums eventually reach 1.

leetcodehash-setmathcycle-detection
LeetCode 177: Nth Highest Salary

A clear SQL solution for finding the nth highest distinct salary from the Employee table.

leetcodesqldatabasedistinctlimit-offset
LeetCode 252: Meeting Rooms

A clear explanation of the Meeting Rooms problem using interval sorting to detect overlaps.

leetcodeintervalssortinggreedy
LeetCode 227: Basic Calculator II

A detailed explanation of evaluating arithmetic expressions with stack-based parsing and operator precedence.

leetcodestackstringparsingmath
LeetCode 152: Maximum Product Subarray

A detailed explanation of tracking both maximum and minimum products while scanning the array.

leetcodearraydynamic-programming
LeetCode 78: Subsets

A detailed guide to solving Subsets with backtracking and the include-or-skip recursion idea.

leetcodearraybacktrackingbit-manipulation
LeetCode 28: Find the Index of the First Occurrence in a String

A clear explanation of finding the first occurrence of one string inside another using direct string matching.

leetcodestringstring-matchingtwo-pointers
LeetCode 53: Maximum Subarray

A clear guide to solving Maximum Subarray with brute force first, then Kadane's dynamic programming algorithm.

leetcodearraydynamic-programmingkadane
LeetCode 13: Roman to Integer

A detailed explanation of converting a Roman numeral string into an integer using symbol values and the subtraction rule.

leetcodehash-tablemathstring
LeetCode 477: Total Hamming Distance

A clear explanation of computing the total Hamming distance across all pairs by counting different bits column by column.

leetcodearraybit-manipulationhamming-distance
LeetCode 476: Number Complement

A clear explanation of finding the bitwise complement of a positive integer using a binary mask.

leetcodebit-manipulationbinarymask
LeetCode 451: Sort Characters By Frequency

A clear explanation of sorting characters by decreasing frequency using a hash map and sorting.

leetcodestringhash-mapsortingbucket-sort
LeetCode 426: Convert Binary Search Tree to Sorted Doubly Linked List

Convert a BST into a sorted circular doubly linked list in-place using inorder traversal.

leetcodebinary-search-treetreedfslinked-listinorder-traversal
LeetCode 401: Binary Watch

A clear explanation of the Binary Watch problem using bit counting over all valid times.

leetcodebit-manipulationenumerationbinary-watch
LeetCode 376: Wiggle Subsequence

A clear explanation of the Wiggle Subsequence problem using dynamic programming intuition and an optimized greedy solution.

leetcodearraydynamic-programminggreedy
LeetCode 351: Android Unlock Patterns

A clear explanation of Android Unlock Patterns using backtracking, a jump table, and symmetry optimization.

leetcodebacktrackingdfsmatrixandroid-unlock-patterns
LeetCode 326: Power of Three

A clear explanation of the Power of Three problem using repeated division and integer arithmetic.

leetcodemathrecursionnumber-theory
LeetCode 301: Remove Invalid Parentheses

A clear explanation of Remove Invalid Parentheses using BFS to guarantee the minimum number of removals.

leetcodestringbfsbacktrackingparentheses
LeetCode 276: Paint Fence

A dynamic programming solution for counting ways to paint fence posts with no more than two adjacent posts sharing the same color.

leetcodedynamic-programmingcombinatorics
LeetCode 251: Flatten 2D Vector

A clear explanation of the Flatten 2D Vector problem using row and column pointers to implement an iterator.

leetcodedesignarrayiteratortwo-pointers
LeetCode 226: Invert Binary Tree

A clear explanation of inverting a binary tree using recursive depth-first traversal.

leetcodebinary-treedfsrecursion
LeetCode 201: Bitwise AND of Numbers Range

A clear explanation of finding the bitwise AND of every number in an inclusive range using the common binary prefix.

leetcodebit-manipulationbinarymedium
LeetCode 176: Second Highest Salary

A clear SQL solution for finding the second highest distinct salary from the Employee table.

leetcodesqldatabaseaggregatesubquery
LeetCode 77: Combinations

A detailed guide to solving Combinations with backtracking and pruning.

leetcodebacktrackingrecursioncombinations
LeetCode 52: N-Queens II

A clear guide to solving N-Queens II by counting valid queen placements with backtracking.

leetcodebacktrackingdfsn-queens
LeetCode 27: Remove Element

A clear explanation of removing all occurrences of a value from an array in place using a write pointer.

leetcodearraytwo-pointersin-place
LeetCode 12: Integer to Roman

A detailed explanation of converting an integer into a Roman numeral using a fixed value-symbol table and greedy subtraction.

leetcodehash-tablemathstringgreedy
LeetCode 11: Container With Most Water

A detailed explanation of finding the maximum water container area using two pointers.

leetcodearraytwo-pointersgreedy
LeetCode 10: Regular Expression Matching

A detailed explanation of matching a full string against a simplified regular expression with dot and star using dynamic programming.

leetcodestringdynamic-programmingrecursionmemoization
LeetCode 9: Palindrome Number

A detailed explanation of checking whether an integer is a palindrome using digit operations without converting it to a string.

leetcodemathinteger
LeetCode 8: String to Integer (atoi)

A detailed explanation of parsing a string into a 32-bit signed integer with whitespace, sign, digit reading, and clamping rules.

leetcodestringparsingsimulation
LeetCode 7: Reverse Integer

A detailed explanation of reversing a signed 32-bit integer while handling overflow correctly.

leetcodemathintegeroverflow
LeetCode 6: Zigzag Conversion

A detailed explanation of converting a string into a zigzag pattern using row simulation.

leetcodestringsimulation
LeetCode 5: Longest Palindromic Substring

A detailed explanation of finding the longest palindromic substring using expand-around-center.

leetcodestringtwo-pointerspalindrome
LeetCode 151: Reverse Words in a String

A clear explanation of reversing word order while removing extra spaces.

leetcodestringtwo-pointers
LeetCode 126: Word Ladder II

Find all shortest word transformation sequences using BFS to build shortest-path parents, then backtracking to reconstruct every answer.

leetcodehardbfsbacktrackinghash-setstringgraph
LeetCode 101: Symmetric Tree

A clear explanation of checking whether a binary tree is symmetric using mirror recursion.

leetcodetreebinary-treedfsrecursion
LeetCode 76: Minimum Window Substring

A detailed guide to solving Minimum Window Substring with a sliding window and frequency counters.

leetcodestringhash-mapsliding-window
LeetCode 51: N-Queens

A clear guide to solving N-Queens with backtracking, row-by-row placement, and constant-time conflict checks.

leetcodebacktrackingarraydfsn-queens
LeetCode 4: Median of Two Sorted Arrays

A detailed explanation of finding the median of two sorted arrays using binary search over partitions.

leetcodearraybinary-searchmedian
LeetCode 26: Remove Duplicates from Sorted Array

A clear explanation of removing duplicates from a sorted array in place using two pointers.

leetcodearraytwo-pointersin-place
LeetCode 3: Longest Substring Without Repeating Characters

A clear explanation of the longest substring problem using sliding window and a hash set.

leetcodestringhash-setsliding-window
LeetCode 2: Add Two Numbers

A detailed explanation of the Add Two Numbers linked list problem, including digit-by-digit addition, carry handling, and linked list construction.

leetcodelinked-listmath
LeetCode 1: Two Sum

A clear explanation of the Two Sum problem using brute force first, then an optimized hash map solution.

leetcodearrayhash-maptwo-sum
Calendar

Browse notes by date

6.21 Lower Bounds for Sorting

Prove that any comparison-based sorting algorithm requires Ω(n log n) comparisons in the worst case using a decision tree argument.

algorithmssortinglower-boundscomplexity
6.20 Inversion Counting

Count pairs of elements in the wrong relative order to measure how far an array is from sorted, using a modified merge sort in O(n log n) time.

algorithmssortinginversionsmerge-sort
6.23 Testing Sort Correctness

Verify both the ordering and permutation properties of sorting implementations using randomized, edge-case, and adversarial test strategies.

algorithmssortingtestingcorrectness
6.22 Parallel Sorting

Distribute sorting work across multiple processors to reduce wall-clock time, with analysis of total work, span, communication, and synchronization.

algorithmssortingparallelconcurrency
6.25 Choosing the Right Sort

Select the appropriate sorting algorithm based on input size, data characteristics, memory constraints, and required guarantees such as stability or worst-case bounds.

algorithmssortingdesigntrade-offs
6.24 Common Bugs

Identify boundary errors, broken invariants, and comparator mistakes that cause sorting implementations to fail on edge cases or duplicate-heavy inputs.

algorithmssortingbugscorrectness
6.19 Coordinate Compression

Replace large or sparse keys with small dense ranks that preserve order, making range-based and indexed structures practical on wide-valued data.

algorithmssortingcoordinate-compressionranking
6.11 Partial Sorting

Produce only the smallest k elements in sorted order rather than sorting the entire array, reducing unnecessary work when the full order is not needed.

algorithmssortingpartial-sortselection
6.14 Median Selection

Find the middle value of a collection in linear time using selection algorithms, without the overhead of a full sort.

algorithmssortingmedianselection
6.12 Top k Selection

Find the largest or smallest k elements without sorting the full input, using a heap or partition-based approach.

algorithmssortingselectiontop-kheaps
6.15 External Sorting

Sort datasets that exceed main memory by organizing the algorithm around sequential disk access, merge passes, and minimizing I/O operations.

algorithmssortingexternal-sortingsystems
6.18 Sorting Records

Sort structured values by one or more fields while moving the full record, with attention to key extraction, stability, and multi-key ordering.

algorithmssortingrecordsmulti-key
6.16 Nearly Sorted Data

Exploit near-sorted structure in inputs like append-only logs or incremental updates to sort in linear or near-linear time.

algorithmssortingadaptivenearly-sorted
6.17 Custom Comparators

Define correct comparison relations for user-defined types and non-trivial orderings — consistency requirements that sorting correctness depends on.

algorithmssortingcomparatorscorrectness
6.13 Quickselect

Find the element at a given rank using quicksort's partition step but recursing into only one side, achieving expected linear time.

algorithmssortingquickselectselection
6.7 Counting Sort

Sort integer keys from a small range in linear time by counting occurrences and reconstructing the output from those counts.

algorithmssortingcounting-sortlinear-time
6.3 Insertion Sort

Build a sorted prefix one element at a time by inserting each new element into its correct position within the already-sorted portion.

algorithmssortinginsertion-sort
6.6 Heap Sort

Build a max-heap in place, then repeatedly extract the maximum to produce a sorted array in O(n log n) worst-case time.

algorithmssortingheap-sortheaps
6.4 Merge Sort

Divide the input into halves, recursively sort each half, then merge them — combining local order into global order in O(n log n) time.

algorithmssortingmerge-sortdivide-and-conquer
6.9 Bucket Sort

Distribute elements into buckets by value range, sort each bucket, then concatenate — achieving linear expected time on uniformly distributed input.

algorithmssortingbucket-sortlinear-time
6.5 Quick Sort

Partition around a pivot so smaller elements go left and larger go right, then recursively sort each partition in expected O(n log n) time.

algorithmssortingquicksortdivide-and-conquer
6.10 Stable Sorting

A stable sort preserves the original relative order of equal keys — an extra guarantee required when sorting by secondary fields or compound criteria.

algorithmssortingstability
6.8 Radix Sort

Sort keys digit by digit using a stable subroutine, achieving linear time for fixed-width integers without any key comparisons.

algorithmssortingradix-sortlinear-time
6.2 Selection Sort

Sort by repeatedly selecting the minimum element from the unsorted suffix and placing it into the next output position.

algorithmssortingselection-sort
6.1 Sorting Contracts

A sorting algorithm is correct only when its output is both ordered and a permutation of the input — two properties every implementation must preserve.

algorithmssortingcorrectness
5.14 Consistent Hashing

Distribute keys across a dynamic set of nodes so that adding or removing nodes moves only a minimal fraction of keys.

algorithmshashingdistributed-systemsload-balancing
5.20 Testing Hash Logic

Verify correctness, stability, and performance of hash-based structures through randomized and adversarial test strategies.

algorithmshashingtestingcorrectness
5.18 Attack Resistance

Defend hash tables against adversarial inputs that force worst-case collision behavior using randomized hashing.

algorithmshashingsecurityadversarialrandomization
5.19 Deterministic Hashing

Produce stable hash values that remain consistent across program runs, machines, builds, and language runtimes.

algorithmshashingdeterminismreproducibility
5.22 Hybrid Hash Structures

Combine hash tables with other data structures to handle skewed distributions, heavy deletions, and mixed workloads.

algorithmshashingdata-structureshybriddesign
5.23 Cache-Aware Hashing

Design hash table layouts that minimize cache misses and align memory access patterns with hardware behavior.

algorithmshashingcacheperformancesystems
5.15 Hash Joins

Join two collections by key using a hash table to reduce the cost from quadratic to linear expected time.

algorithmshashingdatabasesjoins
5.25 Case Studies

Examine hash-based structures in complete systems: streaming pipelines, graph algorithms, caches, and distributed workflows.

algorithmshashingcase-studiessystemsstreaming
5.17 Cache Behavior

Understand how memory hierarchy effects cause hash table performance to deviate from asymptotic expectations.

algorithmshashingperformancecachesystems
5.16 Hash-Based Deduplication

Remove duplicate entries from a dataset or stream efficiently using hash sets for membership tracking.

algorithmshashingdeduplicationdata-structures
5.13 Count-Min Sketch

Approximate frequency counting for large key streams using a compact probabilistic data structure with bounded error.

algorithmshashingprobabilisticdata-structuresstreaming
5.24 Real-World Hash Table Design

Choose and implement hash tables that perform reliably under mixed key types, uneven access patterns, and adversarial input.

algorithmshashingdesignsystemsperformance
5.21 Consistent Performance Guarantees

Achieve predictable worst-case bounds for hash-based structures rather than relying solely on average-case expectations.

algorithmshashingperformanceguaranteesworst-case
5.10 Composite Keys

Hash and compare multi-field keys correctly by combining all fields that participate in equality into the hash function.

algorithmshashingcomposite-keyshash-functions
5.6 Sets

Implement a hash-based set for fast membership testing, insertion, and deletion without associated values.

algorithmshashingsetsdata-structures
5.11 Rolling Hashes

Compute hash values for sliding windows over a sequence in constant time by incrementally updating rather than recomputing.

algorithmshashingrolling-hashstringssliding-window
5.7 Maps

Build a hash-based map that associates keys with values and supports insert, lookup, delete, and update in expected constant time.

algorithmshashingmapsdata-structures
5.8 Counting Maps

Track frequency counts for keys using a hash map that increments a counter on each insertion of an existing key.

algorithmshashingcountingfrequencymaps
5.12 Bloom Filters

Test set membership approximately using a compact bit array and multiple hash functions, with no false negatives and bounded false positives.

algorithmshashingbloom-filterprobabilisticdata-structures
5.9 Grouping Keys

Partition a collection into groups by key using a hash map that accumulates values into per-key lists or sets.

algorithmshashinggroupingmapsdata-structures
5.3 Collision Handling

Resolve hash collisions using separate chaining or open addressing, with trade-offs in memory, locality, and load tolerance.

algorithmshashingcollision-handlingdata-structures
5.4 Load Factor and Resizing

Control hash table performance by monitoring the load factor and growing the bucket array before collisions accumulate.

algorithmshashingload-factorresizingperformance