brain

tamnd's digital brain — notes, problems, research

42791 notes

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
LeetCode 103: Binary Tree Zigzag Level Order Traversal

A clear explanation of zigzag level order traversal using breadth-first search and alternating level direction.

leetcodetreebinary-treebfsqueue
LeetCode 80: Remove Duplicates from Sorted Array II

A detailed guide to solving Remove Duplicates from Sorted Array II with an in-place two-pointer method.

leetcodearraytwo-pointersin-place
LeetCode 229: Majority Element II

A clear explanation of finding all elements that appear more than n/3 times using the extended Boyer-Moore voting algorithm.

leetcodearrayhash-tableboyer-moorecounting
LeetCode 30: Substring with Concatenation of All Words

A clear explanation of finding all starting indices where a substring is formed by concatenating every word exactly once.

leetcodehash-tablestringsliding-window
LeetCode 55: Jump Game

A clear guide to solving Jump Game with greedy reachability.

leetcodearraygreedydynamic-programming
LeetCode 203: Remove Linked List Elements

A clear explanation of removing all linked list nodes with a target value using iteration and a dummy node.

leetcodelinked-listiteration
LeetCode 278: First Bad Version

A binary search solution for finding the first bad version while minimizing calls to the isBadVersion API.

leetcodebinary-searchinteractive
LeetCode 157: Read N Characters Given Read4

A clear explanation of implementing read using the given read4 API and copying only the needed characters.

leetcodearraysimulationinteractive
LeetCode 333: Largest BST Subtree

A clear explanation of Largest BST Subtree using postorder traversal and subtree state propagation.

leetcodetreebinary-search-treedfspostorder
LeetCode 382: Linked List Random Node

A clear explanation of selecting a random linked list node with equal probability using reservoir sampling.

leetcodelinked-listmathreservoir-samplingrandomized
LeetCode 431: Encode N-ary Tree to Binary Tree

Convert an N-ary tree into a binary tree and reconstruct it using the left-child right-sibling representation.

leetcodetreen-ary-treebinary-treedfsencoding
LeetCode 19: Remove Nth Node From End of List

A detailed explanation of removing the nth node from the end of a singly linked list using two pointers and a dummy node.

leetcodelinked-listtwo-pointers
LeetCode 332: Reconstruct Itinerary

A clear explanation of Reconstruct Itinerary using a directed graph and Hierholzer's algorithm.

leetcodegraphdfseulerian-pathhierholzer
LeetCode 156: Binary Tree Upside Down

A clear explanation of flipping a binary tree upside down by rewiring pointers from the left spine.

leetcodebinary-treedfsrecursion
LeetCode 130: Surrounded Regions

Capture surrounded O regions by marking border-connected O cells first, then flipping the remaining O cells.

leetcodemediumarraymatrixdfsbfsunion-find
LeetCode 381: Insert Delete GetRandom O(1) - Duplicates Allowed

A clear explanation of designing a randomized multiset with average O(1) insert, remove, and getRandom operations.

leetcodearrayhash-tabledesignrandomized
LeetCode 182: Duplicate Emails

A clear SQL solution for reporting email values that appear more than once in the Person table.

leetcodesqldatabasegroup-byhaving
LeetCode 18: 4Sum

A detailed explanation of finding all unique quadruplets that sum to a target using sorting and two pointers.

leetcodearraytwo-pointerssorting
LeetCode 331: Verify Preorder Serialization of a Binary Tree

A clear explanation of verifying preorder serialization using slot counting without reconstructing the tree.

leetcodetreebinary-treestringstack
LeetCode 455: Assign Cookies

A clear explanation of the greedy two-pointer solution for maximizing the number of content children.

leetcodearraygreedysortingtwo-pointers
LeetCode 181: Employees Earning More Than Their Managers

A clear SQL solution for finding employees whose salary is greater than their manager's salary using a self join.

leetcodesqldatabaseself-join
LeetCode 430: Flatten a Multilevel Doubly Linked List

Flatten a multilevel doubly linked list in-place using depth-first traversal and pointer splicing.

leetcodelinked-listdoubly-linked-listdfsrecursion
LeetCode 17: Letter Combinations of a Phone Number

A detailed explanation of generating all possible phone keypad letter combinations using backtracking.

leetcodestringbacktrackingdepth-first-search
LeetCode 305: Number of Islands II

A clear explanation of Number of Islands II using Union-Find to dynamically merge connected land cells.

leetcodegraphunion-finddisjoint-setmatrix
LeetCode 355: Design Twitter

A clear explanation of implementing a simplified Twitter using hash maps, sets, timestamps, and a heap.

leetcodedesignhash-tableheappriority-queue
LeetCode 330: Patching Array

A clear explanation of Patching Array using a greedy smallest-missing-sum invariant.

leetcodearraygreedy
LeetCode 129: Sum Root to Leaf Numbers

Compute the sum of all numbers formed by root-to-leaf paths using depth-first search and decimal accumulation.

leetcodemediumtreedfsbinary-treerecursion
LeetCode 429: N-ary Tree Level Order Traversal

Traverse an N-ary tree level by level using breadth-first search.

leetcodetreen-ary-treebfsqueuelevel-order-traversal
LeetCode 380: Insert Delete GetRandom O(1)

A clear explanation of designing a randomized set with average O(1) insert, remove, and getRandom operations.

leetcodearrayhash-tabledesignrandomized
LeetCode 480: Sliding Window Median

A clear explanation of maintaining the median of each fixed-size window using two heaps and lazy deletion.

leetcodearrayhash-tablesliding-windowheap
LeetCode 155: Min Stack

A clear explanation of designing a stack that can return the current minimum element in constant time.

leetcodestackdesign
LeetCode 180: Consecutive Numbers

A clear SQL solution for finding numbers that appear at least three times consecutively in the Logs table.

leetcodesqldatabaseself-joinwindow-function
LeetCode 304: Range Sum Query 2D - Immutable

A clear explanation of Range Sum Query 2D - Immutable using a 2D prefix sum matrix for constant-time rectangle queries.

leetcodematrixprefix-sumdynamic-programming
LeetCode 255: Verify Preorder Sequence in Binary Search Tree

A clear explanation of the Verify Preorder Sequence in Binary Search Tree problem using a monotonic stack and lower bound tracking.

leetcodetreebinary-search-treestackmonotonic-stack
LeetCode 354: Russian Doll Envelopes

A clear explanation of solving Russian Doll Envelopes using sorting and longest increasing subsequence.

leetcodedynamic-programmingbinary-searchlissorting
LeetCode 454: 4Sum II

A clear explanation of counting zero-sum tuples across four arrays using pair sums and a hash map.

leetcodearrayhash-maptwo-sumcounting
LeetCode 16: 3Sum Closest

A detailed explanation of finding the sum of three integers closest to a target using sorting and two pointers.

leetcodearraytwo-pointerssorting
LeetCode 329: Longest Increasing Path in a Matrix

A clear explanation of Longest Increasing Path in a Matrix using DFS with memoization.

leetcodematrixdfsdynamic-programmingmemoization
LeetCode 379: Design Phone Directory

A clear explanation of designing a phone directory that can allocate, check, and release numbers efficiently.

leetcodedesignhash-setqueue
LeetCode 128: Longest Consecutive Sequence

Find the longest run of consecutive integers in an unsorted array using a hash set and sequence-start detection.

leetcodemediumarrayhash-sethash-table
LeetCode 179: Largest Number

A clear explanation of arranging non-negative integers to form the largest possible concatenated number using a custom sort order.

leetcodearraystringsortinggreedy
LeetCode 479: Largest Palindrome Product

A clear explanation of finding the largest palindrome made from the product of two n-digit numbers by generating palindrome candidates directly.

leetcodemathenumerationpalindrome
LeetCode 154: Find Minimum in Rotated Sorted Array II

A clear explanation of finding the minimum element in a rotated sorted array that may contain duplicates.

leetcodearraybinary-search
LeetCode 254: Factor Combinations

A clear explanation of the Factor Combinations problem using DFS backtracking with non-decreasing factors.

leetcodebacktrackingdfsmathfactorization
LeetCode 453: Minimum Moves to Equal Array Elements

A clear explanation of the math behind making all array elements equal by incrementing n - 1 elements at a time.

leetcodearraymath
LeetCode 428: Serialize and Deserialize N-ary Tree

Serialize an N-ary tree into a string and reconstruct the same tree using preorder traversal with child counts.

leetcodetreen-ary-treedfsserializationrecursion
LeetCode 303: Range Sum Query - Immutable

A clear explanation of Range Sum Query - Immutable using prefix sums for constant-time range queries.

leetcodearraydesignprefix-sum
LeetCode 353: Design Snake Game

A clear explanation of implementing Snake Game with a deque for body order and a set for constant-time collision checks.

leetcodedesigndequehash-setmatrix
LeetCode 15: 3Sum

A detailed explanation of finding all unique triplets that sum to zero using sorting and two pointers.

leetcodearraytwo-pointerssorting
LeetCode 127: Word Ladder

Use breadth-first search to find the shortest transformation sequence length between two words.

leetcodemediumbfsgraphstringhash-set
LeetCode 102: Binary Tree Level Order Traversal

A clear explanation of binary tree level order traversal using breadth-first search and a queue.

leetcodetreebinary-treebfsqueue
LeetCode 403: Frog Jump

A clear explanation of the Frog Jump problem using dynamic programming with reachable jump sizes.

leetcodedynamic-programminghash-setdfsmemoization
LeetCode 328: Odd Even Linked List

A clear explanation of Odd Even Linked List using in-place pointer rewiring.

leetcodelinked-listtwo-pointers
LeetCode 378: Kth Smallest Element in a Sorted Matrix

A clear explanation of finding the kth smallest value in a row-sorted and column-sorted matrix using binary search on values.

leetcodearraymatrixbinary-search
LeetCode 178: Rank Scores

A clear SQL solution for ranking scores with dense ranking, where ties share the same rank and no rank numbers are skipped.

leetcodesqldatabasewindow-functiondense-rank
LeetCode 478: Generate Random Point in a Circle

A clear explanation of generating uniformly random points inside a circle using polar coordinates.

leetcodemathgeometryrandomizedrejection-sampling
LeetCode 253: Meeting Rooms II

A clear explanation of the Meeting Rooms II problem using a min heap to track active meeting end times.

leetcodeintervalsheappriority-queuesorting
LeetCode 228: Summary Ranges

A clear explanation of summarizing a sorted unique integer array into compact consecutive ranges.

leetcodearraytwo-pointers
LeetCode 79: Word Search

A detailed guide to solving Word Search with depth-first search and backtracking on a grid.

leetcodearraystringbacktrackingdepth-first-searchmatrix
LeetCode 153: Find Minimum in Rotated Sorted Array

A clear explanation of finding the minimum element in a rotated sorted array using binary search.

leetcodearraybinary-search
LeetCode 29: Divide Two Integers

A clear explanation of integer division without using multiplication, division, or modulo, using repeated doubling with bit shifts.

leetcodemathbit-manipulationbinary-search-style-doubling
LeetCode 14: Longest Common Prefix

A detailed explanation of finding the longest common prefix among an array of strings by comparing characters column by column.

leetcodestring
LeetCode 54: Spiral Matrix

A clear guide to reading a matrix in spiral order using shrinking boundaries.

leetcodearraymatrixsimulation
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