brain

tamnd's digital brain — notes, problems, research

43815 notes

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 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 334: Increasing Triplet Subsequence

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

leetcodearraygreedy
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 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 127: Word Ladder

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

leetcodemediumbfsgraphstringhash-set
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