brain

tamnd's digital brain — notes, problems, research

42778 notes

LeetCode 162: Find Peak Element

A clear explanation of finding any peak element using binary search on the slope of the array.

leetcodearraybinary-search
LeetCode 311: Sparse Matrix Multiplication

A clear explanation of Sparse Matrix Multiplication using non-zero entries to avoid wasted work.

leetcodematrixhash-tablesimulation
LeetCode 486: Predict the Winner

A clear explanation of predicting whether Player 1 can win using minimax dynamic programming over score difference.

leetcodearraydynamic-programminggame-theoryrecursion
LeetCode 340: Longest Substring with At Most K Distinct Characters

A clear explanation of Longest Substring with At Most K Distinct Characters using a sliding window and character counts.

leetcodestringhash-tablesliding-window
LeetCode 339: Nested List Weight Sum

A clear explanation of Nested List Weight Sum using depth-first search over a nested structure.

leetcodedepth-first-searchbreadth-first-searchrecursion
LeetCode 338: Counting Bits

A clear explanation of Counting Bits using dynamic programming and bit manipulation.

leetcodedynamic-programmingbit-manipulation
LeetCode 282: Expression Add Operators

A backtracking solution for inserting operators into a numeric string so the expression evaluates to a target value.

leetcodebacktrackingdfsstringrecursion
LeetCode 460: LFU Cache

A clear explanation of designing an LFU cache with O(1) average get and put operations.

leetcodehash-maplinked-listdesignlfu-cache
LeetCode 459: Repeated Substring Pattern

A clear explanation of checking whether a string can be built by repeating one of its proper substrings.

leetcodestringstring-matching
LeetCode 60: Permutation Sequence

A clear guide to finding the kth permutation sequence using factorial blocks instead of generating all permutations.

leetcodemathrecursionpermutation
LeetCode 361: Bomb Enemy

A clear explanation of finding the best bomb placement in a grid using cached row and column segment counts.

leetcodedynamic-programmingmatrixgrid
LeetCode 360: Sort Transformed Array

A clear explanation of sorting values after applying a quadratic function using two pointers.

leetcodearraymathtwo-pointerssorting
LeetCode 84: Largest Rectangle in Histogram

A detailed guide to solving Largest Rectangle in Histogram with a monotonic increasing stack.

leetcodearraystackmonotonic-stack
LeetCode 436: Find Right Interval

Find, for each interval, the interval with the smallest start point greater than or equal to its end point using sorting and binary search.

leetcodearraybinary-searchsortingintervals
LeetCode 261: Graph Valid Tree

A clear explanation of the Graph Valid Tree problem using Union Find to detect cycles and verify connectivity.

leetcodegraphunion-finddfstree
LeetCode 234: Palindrome Linked List

A clear explanation of checking whether a singly linked list is a palindrome using fast and slow pointers plus in-place reversal.

leetcodelinked-listtwo-pointersrecursion
LeetCode 134: Gas Station

Find the unique starting gas station index using a greedy scan with total fuel balance and current tank balance.

leetcodemediumarraygreedy
LeetCode 208: Implement Trie Prefix Tree

A clear explanation of implementing a Trie with insert, search, and startsWith operations.

leetcodetrieprefix-treedesignstring
LeetCode 186: Reverse Words in a String II

A clear explanation of reversing the order of words in a character array in-place using two reversals.

leetcodestringarraytwo-pointersin-place
LeetCode 35: Search Insert Position

A clear explanation of finding the index of a target, or where it should be inserted, using binary search.

leetcodearraybinary-search
LeetCode 386: Lexicographical Numbers

A clear explanation of generating numbers from 1 to n in lexicographical order using an iterative DFS-style traversal.

leetcodedepth-first-searchtrieiteration
LeetCode 408: Valid Word Abbreviation

A clear explanation of validating a word abbreviation using two pointers and number parsing.

leetcodestringtwo-pointerssimulation
LeetCode 161: One Edit Distance

A clear explanation of checking whether two strings are exactly one edit apart using a linear scan.

leetcodestringtwo-pointers
LeetCode 485: Max Consecutive Ones

A clear explanation of finding the longest streak of 1s in a binary array with a single pass.

leetcodearraysimulation
LeetCode 310: Minimum Height Trees

A clear explanation of Minimum Height Trees using leaf trimming to find the center of a tree.

leetcodegraphtreebfstopological-sort
LeetCode 337: House Robber III

A clear explanation of House Robber III using tree dynamic programming with rob and skip states.

leetcodetreedfsdynamic-programmingbinary-tree
LeetCode 59: Spiral Matrix II

A clear guide to generating an n x n matrix filled from 1 to n squared in spiral order.

leetcodearraymatrixsimulation
LeetCode 260: Single Number III

A clear explanation of the Single Number III problem using XOR partitioning to isolate the two unique numbers.

leetcodebit-manipulationxor
LeetCode 359: Logger Rate Limiter

A clear explanation of designing a logger that prints each message at most once every 10 seconds using a hash map.

leetcodedesignhash-tabledata-stream
LeetCode 233: Number of Digit One

A detailed explanation of counting how many times digit one appears from 0 to n using positional digit analysis.

leetcodemathdigit-dpcounting
LeetCode 83: Remove Duplicates from Sorted List

A detailed guide to solving Remove Duplicates from Sorted List with one pointer and in-place linked list rewiring.

leetcodelinked-list
LeetCode 281: Zigzag Iterator

A queue-based iterator design for returning elements from two vectors in alternating order, with a clean extension to k vectors.

leetcodedesignqueueiteratorarray
LeetCode 435: Non-overlapping Intervals

Remove the minimum number of intervals so the remaining intervals do not overlap, using greedy sorting by end time.

leetcodearrayintervalsgreedysorting
LeetCode 434: Number of Segments in a String

Count the number of word segments in a string by detecting transitions from spaces to non-space characters.

leetcodestringsimulation
LeetCode 133: Clone Graph

Create a deep copy of a connected undirected graph using DFS and a hash map from original nodes to cloned nodes.

leetcodemediumgraphdfsbfshash-map
LeetCode 207: Course Schedule

A clear explanation of detecting cycles in a prerequisite graph using topological sorting and DFS.

leetcodegraphtopological-sortdfsbfs
LeetCode 458: Poor Pigs

A clear explanation of the combinatorics behind finding the minimum number of pigs needed to identify the poisonous bucket.

leetcodemathcombinatorics
LeetCode 34: Find First and Last Position of Element in Sorted Array

A clear explanation of finding the first and last index of a target in a sorted array using two binary searches.

leetcodearraybinary-search
LeetCode 106: Construct Binary Tree from Inorder and Postorder Traversal

A clear explanation of rebuilding a binary tree from inorder and postorder traversals using recursion and an index map.

leetcodetreebinary-treedfsrecursionhash-mapdivide-and-conquer
LeetCode 160: Intersection of Two Linked Lists

A clear explanation of finding the node where two singly linked lists intersect using two pointers.

leetcodelinked-listtwo-pointers
LeetCode 484: Find Permutation

A clear explanation of constructing the lexicographically smallest permutation that matches an I and D pattern.

leetcodearraystringstackgreedy
LeetCode 309: Best Time to Buy and Sell Stock with Cooldown

A clear explanation of Best Time to Buy and Sell Stock with Cooldown using dynamic programming states.

leetcodearraydynamic-programmingstock
LeetCode 259: 3Sum Smaller

A clear explanation of the 3Sum Smaller problem using sorting and the two-pointer technique.

leetcodearraytwo-pointerssorting
LeetCode 58: Length of Last Word

A clear guide to solving Length of Last Word by scanning the string from right to left.

leetcodestringtwo-pointers
LeetCode 385: Mini Parser

A clear explanation of parsing a serialized nested integer string using a stack.

leetcodestringstackparserdesign
LeetCode 407: Trapping Rain Water II

A clear explanation of trapping rain water in a 2D elevation map using a min heap and boundary expansion.

leetcodeheappriority-queuebfsmatrixgraph
LeetCode 232: Implement Queue using Stacks

A detailed explanation of implementing a FIFO queue using two LIFO stacks with amortized constant time operations.

leetcodestackqueuedesigndata-structure
LeetCode 185: Department Top Three Salaries

A clear SQL solution for finding employees whose salaries are in the top three unique salary levels within their department.

leetcodesqldatabasewindow-functiondense-rank
LeetCode 358: Rearrange String k Distance Apart

A clear explanation of rearranging a string so equal characters are at least k positions apart using a heap and cooldown queue.

leetcodestringgreedyheapqueue
LeetCode 336: Palindrome Pairs

A clear explanation of Palindrome Pairs using reversed-word lookup and palindrome split checks.

leetcodearrayhash-tablestringtrie
LeetCode 206: Reverse Linked List

A clear explanation of reversing a singly linked list using iterative and recursive approaches.

leetcodelinked-listrecursion
LeetCode 82: Remove Duplicates from Sorted List II

A detailed guide to solving Remove Duplicates from Sorted List II with a dummy node and pointer rewiring.

leetcodelinked-listtwo-pointers
LeetCode 132: Palindrome Partitioning II

Find the minimum number of cuts needed to split a string into palindromic substrings using palindrome precomputation and dynamic programming.

leetcodehardstringdynamic-programmingpalindrome
LeetCode 280: Wiggle Sort

A greedy in-place solution for rearranging an array into a non-strict wiggle pattern.

leetcodearraygreedysorting
LeetCode 433: Minimum Genetic Mutation

Find the minimum number of valid one-character gene mutations using breadth-first search.

leetcodebfshash-setstringshortest-path
LeetCode 25: Reverse Nodes in k-Group

A detailed explanation of reversing linked-list nodes in groups of k using pointer manipulation and constant extra space.

leetcodelinked-listrecursion
LeetCode 24: Swap Nodes in Pairs

A detailed explanation of swapping every two adjacent nodes in a linked list using pointer manipulation.

leetcodelinked-listrecursion
LeetCode 23: Merge k Sorted Lists

A detailed explanation of merging k sorted linked lists using a min heap.

leetcodelinked-listheappriority-queuedivide-and-conquer
LeetCode 33: Search in Rotated Sorted Array

A clear explanation of searching a rotated sorted array in logarithmic time using modified binary search.

leetcodearraybinary-search
LeetCode 308: Range Sum Query 2D - Mutable

A clear explanation of Range Sum Query 2D - Mutable using a 2D Fenwick Tree for efficient updates and rectangle sum queries.

leetcodematrixfenwick-treebinary-indexed-treedesign
LeetCode 406: Queue Reconstruction by Height

A clear explanation of reconstructing a queue using greedy sorting and indexed insertion.

leetcodegreedysortingarray
LeetCode 258: Add Digits

A clear explanation of the Add Digits problem using repeated digit sums first, then the digital root formula.

leetcodemathsimulationnumber-theory
LeetCode 483: Smallest Good Base

A clear explanation of finding the smallest base where n is written as all ones using geometric series and binary search.

leetcodemathbinary-searchgeometric-series
LeetCode 231: Power of Two

A clear explanation of determining whether an integer is a power of two using binary properties and bit manipulation.

leetcodemathbit-manipulationbinary
LeetCode 105: Construct Binary Tree from Preorder and Inorder Traversal

A clear explanation of rebuilding a binary tree from preorder and inorder traversals using recursion and an index map.

leetcodetreebinary-treedfsrecursionhash-map
LeetCode 457: Circular Array Loop

A clear explanation of detecting a valid cycle in a circular array using fast and slow pointers.

leetcodearraytwo-pointerscycle-detection
LeetCode 159: Longest Substring with At Most Two Distinct Characters

A clear explanation of finding the longest substring with at most two distinct characters using a sliding window.

leetcodestringhash-tablesliding-window
LeetCode 384: Shuffle an Array

A clear explanation of shuffling an array uniformly using the Fisher-Yates algorithm while supporting reset.

leetcodearraymathdesignrandomized
LeetCode 205: Isomorphic Strings

A clear explanation of checking whether two strings follow the same character mapping pattern.

leetcodehash-mapstring
LeetCode 57: Insert Interval

A clear guide to solving Insert Interval with one linear scan over sorted, non-overlapping intervals.

leetcodearrayintervals
LeetCode 335: Self Crossing

A clear explanation of Self Crossing using constant-space checks for the only possible crossing patterns.

leetcodearraygeometrymath
LeetCode 32: Longest Valid Parentheses

A clear explanation of finding the longest well-formed parentheses substring using a stack of indices.

leetcodestringstackdynamic-programming
LeetCode 357: Count Numbers with Unique Digits

A clear explanation of counting numbers with unique digits using combinatorics.

leetcodemathdynamic-programmingcombinatorics
LeetCode 184: Department Highest Salary

A clear SQL solution for finding every employee who earns the highest salary in their department.

leetcodesqldatabasejoingroup-bywindow-function
LeetCode 307: Range Sum Query - Mutable

A clear explanation of Range Sum Query - Mutable using a Fenwick Tree for efficient updates and range sums.

leetcodearrayfenwick-treebinary-indexed-treesegment-tree
LeetCode 81: Search in Rotated Sorted Array II

A detailed guide to solving Search in Rotated Sorted Array II with modified binary search and duplicate handling.

leetcodearraybinary-search
LeetCode 405: Convert a Number to Hexadecimal

A clear explanation of converting integers to hexadecimal using bit manipulation and two's complement representation.

leetcodebit-manipulationmathhexadecimal
LeetCode 22: Generate Parentheses

A detailed explanation of generating all well-formed parentheses strings using backtracking.

leetcodestringbacktrackingdepth-first-search
LeetCode 21: Merge Two Sorted Lists

A detailed explanation of merging two sorted linked lists using a dummy node and pointer splicing.

leetcodelinked-listrecursion
LeetCode 482: License Key Formatting

A clear explanation of reformatting a license key by removing dashes, uppercasing characters, and grouping from the right.

leetcodestringsimulation
LeetCode 257: Binary Tree Paths

A clear explanation of the Binary Tree Paths problem using DFS backtracking to collect every root-to-leaf path.

leetcodetreebinary-treedfsbacktrackingstring
LeetCode 104: Maximum Depth of Binary Tree

A clear explanation of finding the maximum depth of a binary tree using recursive depth-first search.

leetcodetreebinary-treedfsrecursion
LeetCode 230: Kth Smallest Element in a BST

A clear explanation of finding the kth smallest value in a binary search tree using inorder traversal.

leetcodetreebinary-search-treedfsstack
LeetCode 383: Ransom Note

A clear explanation of checking whether one string can be constructed from another using character frequency counting.

leetcodestringhash-tablecounting
LeetCode 204: Count Primes

A clear explanation of counting prime numbers less than n using the Sieve of Eratosthenes.

leetcodemathprimesieve
LeetCode 56: Merge Intervals

A clear guide to solving Merge Intervals by sorting intervals and merging them in one pass.

leetcodearraysortingintervals
LeetCode 279: Perfect Squares

A dynamic programming solution for finding the least number of perfect square numbers that sum to n.

leetcodedynamic-programmingmathbreadth-first-search
LeetCode 158: Read N Characters Given read4 II - Call Multiple Times

A clear explanation of implementing read with read4 when read may be called multiple times.

leetcodearraysimulationinteractive
LeetCode 334: Increasing Triplet Subsequence

A clear explanation of Increasing Triplet Subsequence using greedy tracking of two minimum values.

leetcodearraygreedy
LeetCode 432: All O'one Data Structure

Design a data structure that supports increment, decrement, get minimum key, and get maximum key in average O(1) time.

leetcodehash-tablelinked-listdoubly-linked-listdesign
LeetCode 131: Palindrome Partitioning

Generate all ways to split a string so that every piece is a palindrome, using backtracking with palindrome precomputation.

leetcodemediumstringbacktrackingdynamic-programming
LeetCode 183: Customers Who Never Order

A clear SQL solution for finding customers who have no matching rows in the Orders table.

leetcodesqldatabaseleft-joinanti-join
LeetCode 456: 132 Pattern

A clear explanation of detecting a 132 pattern using reverse traversal and a monotonic stack.

leetcodearraystackmonotonic-stack
LeetCode 356: Line Reflection

A clear explanation of checking whether 2D points are symmetric around a vertical line using min and max x-coordinates.

leetcodehash-tablegeometryset
LeetCode 31: Next Permutation

A clear explanation of finding the next lexicographically greater permutation in place using a right-to-left scan.

leetcodearraytwo-pointersin-place
LeetCode 20: Valid Parentheses

A detailed explanation of checking whether a bracket string is valid using a stack.

leetcodestringstack
LeetCode 306: Additive Number

A clear explanation of Additive Number using split enumeration and deterministic checking.

leetcodestringbacktrackingenumeration
LeetCode 256: Paint House

A clear explanation of the Paint House problem using dynamic programming with constant space.

leetcodedynamic-programmingarray
LeetCode 481: Magical String

A clear explanation of constructing the magical string by using the string itself as run-length instructions.

leetcodestringtwo-pointerssimulation
LeetCode 404: Sum of Left Leaves

A clear explanation of the Sum of Left Leaves problem using depth-first traversal of a binary tree.

leetcodetreedfsbinary-treerecursion