brain

tamnd's digital brain — notes, problems, research

42768 notes

LeetCode 595: Big Countries

A clear SQL guide for finding countries with either large area or large population.

leetcodesqlwherefiltering
LeetCode 569: Median Employee Salary

A clear explanation of Median Employee Salary using SQL window functions to rank employees inside each company.

leetcodesqldatabasewindow-function
LeetCode 517: Super Washing Machines

A clear explanation of balancing dresses across washing machines using greedy prefix flow.

leetcodearraygreedyprefix-sum
LeetCode 544: Output Contest Matches

A clear explanation of building the final tournament bracket by repeatedly pairing strongest and weakest teams.

leetcodestringsimulationrecursion
LeetCode 594: Longest Harmonious Subsequence

A clear hash map solution for finding the longest subsequence whose maximum and minimum differ by exactly one.

leetcodearrayhash-mapcounting
LeetCode 516: Longest Palindromic Subsequence

A clear explanation of finding the length of the longest palindromic subsequence using interval dynamic programming.

leetcodestringdynamic-programminginterval-dp
LeetCode 543: Diameter of Binary Tree

A clear explanation of finding the longest path between any two nodes in a binary tree using DFS height computation.

leetcodetreebinary-treedepth-first-searchrecursion
LeetCode 568: Maximum Vacation Days

A clear explanation of Maximum Vacation Days using dynamic programming over weeks and cities.

leetcodedynamic-programmingmatrixgraph
LeetCode 515: Find Largest Value in Each Tree Row

A clear explanation of finding the maximum value at every depth of a binary tree using level-order traversal.

leetcodebinary-treetreebfslevel-order-traversal
LeetCode 593: Valid Square

A clear geometry solution for checking whether four unordered points form a valid square.

leetcodemathgeometrysorting
LeetCode 567: Permutation in String

A clear explanation of Permutation in String using a fixed-size sliding window and character frequency counts.

leetcodestringhash-mapsliding-window
LeetCode 542: 01 Matrix

A clear explanation of computing the distance to the nearest zero in a binary matrix using multi-source BFS.

leetcodearraymatrixbreadth-first-searchdynamic-programming
LeetCode 514: Freedom Trail

A clear explanation of finding the minimum steps to spell a key on a circular ring using dynamic programming and memoized DFS.

leetcodedynamic-programmingdfsmemoizationstring
LeetCode 592: Fraction Addition and Subtraction

A clear parsing and math solution for evaluating fraction addition and subtraction expressions.

leetcodemathstringparsinggcd
LeetCode 591: Tag Validator

A clear stack-based parser for validating nested XML-like tags with CDATA sections.

leetcodestackstringparsing
LeetCode 566: Reshape the Matrix

A clear explanation of Reshape the Matrix using index mapping from the original matrix to the reshaped matrix.

leetcodearraymatrixsimulation
LeetCode 513: Find Bottom Left Tree Value

A clear explanation of finding the leftmost value in the deepest row of a binary tree using level-order traversal.

leetcodebinary-treetreebfslevel-order-traversal
LeetCode 512: Game Play Analysis II

A clear explanation of finding the first device used by each player using SQL aggregation and a join.

leetcodedatabasesqlgroup-byjoinaggregation
LeetCode 511: Game Play Analysis I

A clear explanation of finding each player's first login date using SQL aggregation.

leetcodedatabasesqlgroup-byaggregation
LeetCode 590: N-ary Tree Postorder Traversal

A clear DFS solution for returning the postorder traversal of an N-ary tree.

leetcodetreedfsstackrecursion
LeetCode 565: Array Nesting

A clear explanation of Array Nesting using cycle detection over a permutation.

leetcodearraydepth-first-searchcycle-detection
LeetCode 541: Reverse String II

A clear explanation of reversing the first k characters in every 2k block of a string.

leetcodestringtwo-pointerssimulation
LeetCode 510: Inorder Successor in BST II

A clear explanation of finding the inorder successor in a binary search tree when nodes contain parent pointers.

leetcodebinary-search-treetreeinorder-traversal
LeetCode 509: Fibonacci Number

A clear explanation of computing Fibonacci numbers using dynamic programming and iterative state transitions.

leetcodedynamic-programmingmathrecursion
LeetCode 508: Most Frequent Subtree Sum

A clear explanation of finding the most frequent subtree sum in a binary tree using postorder DFS and a frequency map.

leetcodebinary-treetreedfshash-mappostorder-traversal
LeetCode 629: K Inverse Pairs Array

A dynamic programming solution for counting permutations of 1 to n with exactly k inverse pairs.

leetcodedynamic-programmingprefix-summath
LeetCode 655: Print Binary Tree

A clear explanation of formatting a binary tree into a 2D string matrix using tree height and recursive placement.

leetcodetreebinary-treedepth-first-searchrecursionmatrix
LeetCode 605: Can Place Flowers

A greedy guide for determining whether a given number of flowers can be planted without violating the no-adjacent-flowers rule.

leetcodearraygreedy
LeetCode 589: N-ary Tree Preorder Traversal

A clear DFS solution for returning the preorder traversal of an N-ary tree.

leetcodetreedfsstackrecursion
LeetCode 564: Find the Closest Palindrome

A clear explanation of Find the Closest Palindrome using prefix mirroring and a small candidate set.

leetcodemathstringpalindrome
LeetCode 540: Single Element in a Sorted Array

A clear explanation of finding the only non-duplicate element in a sorted array using binary search.

leetcodearraybinary-search
LeetCode 507: Perfect Number

A clear explanation of checking whether a number equals the sum of its positive divisors excluding itself.

leetcodemathnumber-theorydivisors
LeetCode 853: Car Fleet

A clear explanation of counting car fleets by sorting cars by position and tracking arrival times.

leetcodearraysortingstack
LeetCode 803: Bricks Falling When Hit

A reverse simulation and union-find solution for counting how many bricks fall after each hit.

leetcodeunion-finddisjoint-set-uniongridreverse-processing
LeetCode 753: Cracking the Safe

A clear explanation of Cracking the Safe using a de Bruijn sequence and depth-first search over password states.

leetcodedfsgrapheulerian-pathde-bruijn-sequence
LeetCode 728: Self Dividing Numbers

Check each number in a range by extracting its digits and testing whether every digit divides the original number.

leetcodemathsimulation
LeetCode 702: Search in a Sorted Array of Unknown Size

A clear explanation of searching in a sorted array when the array length is hidden behind an ArrayReader interface.

leetcodebinary-searcharrayinteractive
LeetCode 506: Relative Ranks

A clear explanation of assigning athlete ranks from scores using sorting while preserving original indices.

leetcodearraysortinghash-map
LeetCode 678: Valid Parenthesis String

Check whether a string containing parentheses and wildcard stars can be made valid using a greedy range of possible open counts.

leetcodestringgreedystack
LeetCode 539: Minimum Time Difference

A clear explanation of finding the minimum difference between 24-hour clock times using minute conversion and sorting.

leetcodearraystringsorting
LeetCode 604: Design Compressed String Iterator

A guide to implementing a lazy iterator over a run-length encoded string without fully decompressing it.

leetcodedesignstringiteratorarray
LeetCode 563: Binary Tree Tilt

A clear explanation of Binary Tree Tilt using postorder DFS to compute subtree sums and accumulate tilt.

leetcodetreebinary-treedepth-first-searchpostorder
LeetCode 654: Maximum Binary Tree

A clear explanation of constructing a maximum binary tree recursively using divide and conquer.

leetcodetreebinary-treedivide-and-conquerrecursionmonotonic-stack
LeetCode 588: Design In-Memory File System

A clear design guide for implementing an in-memory file system with directory listing, directory creation, file append, and file read operations.

leetcodedesigntriehash-mapstringsorting
LeetCode 628: Maximum Product of Three Numbers

A clear explanation of finding the largest product of three numbers using sorting or constant-space tracking.

leetcodearraymathsorting
LeetCode 505: The Maze II

A clear explanation of finding the shortest rolling distance in a maze using Dijkstra’s algorithm.

leetcodegraphshortest-pathdijkstrabfsmatrix
LeetCode 677: Map Sum Pairs

Design a map that supports key-value insertion and prefix-sum queries using a hash map and trie.

leetcodetriehash-mapstringdesign
LeetCode 562: Longest Line of Consecutive One in Matrix

A clear explanation of Longest Line of Consecutive One in Matrix using dynamic programming over four directions.

leetcodearraymatrixdynamic-programming
LeetCode 603: Consecutive Available Seats

A SQL guide for finding all cinema seats that are free and adjacent to at least one other free seat.

leetcodesqlself-joinwindow-functiondatabase
LeetCode 653: Two Sum IV - Input is a BST

A clear explanation of finding whether two different nodes in a binary search tree sum to a target value.

leetcodetreebinary-treebinary-search-treehash-setdepth-first-search
LeetCode 587: Erect the Fence

A clear convex hull solution for returning all trees that lie on the fence boundary.

leetcodearraygeometryconvex-hullmonotonic-chain
LeetCode 627: Swap Salary

A SQL update solution for swapping all m and f values in the Salary table using a single statement.

leetcodesqlupdatecase
LeetCode 538: Convert BST to Greater Tree

A clear explanation of converting a BST into a greater tree using reverse inorder traversal and a running sum.

leetcodetreebinary-search-treedepth-first-searchinorder-traversal
LeetCode 504: Base 7

A clear explanation of converting an integer into its base 7 string representation using repeated division.

leetcodemathstringnumber-system
LeetCode 503: Next Greater Element II

A clear explanation of finding the next greater element in a circular array using a monotonic stack.

leetcodearraystackmonotonic-stackcircular-array
LeetCode 927: Three Equal Parts

A clear explanation of solving Three Equal Parts by counting ones, locating the three binary patterns, and comparing them in one pass.

leetcodearraybinarygreedy
LeetCode 977: Squares of a Sorted Array

A clear explanation of sorting squared values from a sorted array using two pointers.

leetcodearraytwo-pointerssorting
LeetCode 952: Largest Component Size by Common Factor

A clear explanation of solving Largest Component Size by Common Factor using prime factorization and union find.

leetcodearraymathnumber-theoryunion-find
LeetCode 877: Stone Game

A clear explanation of the Stone Game problem using game theory and interval dynamic programming.

leetcodearraydynamic-programminggame-theoryinterval-dp
LeetCode 902: Numbers At Most N Given Digit Set

A clear explanation of counting numbers less than or equal to N using digit-by-digit construction and combinatorics.

leetcodemathdigit-dpcombinatorics
LeetCode 852: Peak Index in a Mountain Array

A clear explanation of finding the peak index in a mountain array using binary search.

leetcodearraybinary-search
LeetCode 827: Making A Large Island

A clear explanation of the Making A Large Island problem using connected component labeling and island size lookup.

leetcodearraymatrixdepth-first-searchbreadth-first-searchunion-find
LeetCode 802: Find Eventual Safe States

A graph traversal solution for finding all nodes that cannot reach a directed cycle.

leetcodegraphdfscycle-detection
LeetCode 777: Swap Adjacent in LR String

A clear explanation of validating string transformation using two pointers and movement constraints.

leetcodestringtwo-pointers
LeetCode 752: Open the Lock

A clear explanation of solving Open the Lock using breadth-first search over lock states.

leetcodebfsgraphhash-setstring
LeetCode 727: Minimum Window Subsequence

Find the shortest substring of s1 that contains s2 as a subsequence using dynamic programming.

leetcodestringdynamic-programmingsubsequence
LeetCode 976: Largest Perimeter Triangle

A clear explanation of finding the largest valid triangle perimeter using sorting and a greedy scan.

leetcodearraysortinggreedytriangle-inequality
LeetCode 951: Flip Equivalent Binary Trees

A clear explanation of checking whether two binary trees are equivalent after swapping left and right children at any number of nodes.

leetcodetreebinary-treedfsrecursion
LeetCode 926: Flip String to Monotone Increasing

A clear explanation of solving Flip String to Monotone Increasing with a one-pass dynamic programming approach.

leetcodedynamic-programmingstringgreedy
LeetCode 901: Online Stock Span

A clear explanation of Online Stock Span using a monotonic decreasing stack with accumulated spans.

leetcodestackmonotonic-stackdesigndata-stream
LeetCode 876: Middle of the Linked List

A clear explanation of finding the middle node of a singly linked list using slow and fast pointers.

leetcodelinked-listtwo-pointersslow-fast-pointer
LeetCode 851: Loud and Rich

A clear explanation of Loud and Rich using graph traversal, DFS, and memoization.

leetcodegraphdfsmemoizationtopological-sort
LeetCode 826: Most Profit Assigning Work

A clear explanation of the Most Profit Assigning Work problem using sorting, greedy choice, and two pointers.

leetcodearraysortinggreedytwo-pointers
LeetCode 801: Minimum Swaps To Make Sequences Increasing

A dynamic programming solution for finding the minimum number of same-index swaps needed to make two arrays strictly increasing.

leetcodedynamic-programmingarray
LeetCode 776: Split BST

A clear explanation of splitting a binary search tree into two BSTs using recursion and pointer rewiring.

leetcodetreebinary-search-treerecursion
LeetCode 751: IP to CIDR

A clear explanation of converting a range of IPv4 addresses into the shortest list of CIDR blocks using greedy bit manipulation.

leetcodebit-manipulationstringgreedycidr
LeetCode 726: Number of Atoms

Parse a chemical formula with nested parentheses, atom names, and multipliers using recursive descent.

leetcodestringstackhash-mapsortingparsing
LeetCode 701: Insert into a Binary Search Tree

A clear explanation of inserting a value into a binary search tree using recursive and iterative traversal.

leetcodebinary-treebinary-search-treerecursiontree
LeetCode 676: Implement Magic Dictionary

Design a dictionary that can check whether a word can match a stored word after changing exactly one character.

leetcodehash-mapstringdesign
LeetCode 652: Find Duplicate Subtrees

A clear explanation of finding duplicate binary tree subtrees using postorder traversal, serialization, and a hash map.

leetcodetreebinary-treehash-mapdepth-first-searchserialization
LeetCode 602: Friend Requests II: Who Has the Most Friends

A SQL guide for counting friendships from both requester and accepter sides, then returning the user with the most friends.

leetcodesqlunion-allgroup-bydatabase
LeetCode 502: IPO

A clear explanation of maximizing capital by selecting at most k projects using sorting and a max heap.

leetcodegreedyheappriority-queuesorting
LeetCode 537: Complex Number Multiplication

A clear explanation of multiplying complex numbers represented as strings using algebraic expansion.

leetcodemathstringsimulation
LeetCode 586: Customer Placing the Largest Number of Orders

A clear SQL guide for finding the customer who placed the most orders.

leetcodesqlgroup-byorder-byaggregation
LeetCode 561: Array Partition

A clear explanation of Array Partition using sorting and adjacent pairing to maximize the sum of pair minimums.

leetcodearraygreedysorting
LeetCode 651: 4 Keys Keyboard

A dynamic programming solution for maximizing the number of A characters printed with a limited number of keyboard operations.

leetcodedynamic-programmingmath
LeetCode 626: Exchange Seats

A SQL solution for swapping every pair of adjacent student seats while leaving the final seat unchanged when the row count is odd.

leetcodesqlcasesorting
LeetCode 601: Human Traffic of Stadium

A SQL guide for finding stadium records that belong to runs of at least three consecutive ids where each row has at least 100 people.

leetcodesqlwindow-functiongroupingdatabase
LeetCode 501: Find Mode in Binary Search Tree

A clear explanation of finding the most frequent value or values in a binary search tree using inorder traversal.

leetcodebinary-treebinary-search-treedfsinorder-traversal
LeetCode 536: Construct Binary Tree from String

A clear explanation of parsing a parenthesized string recursively to construct a binary tree.

leetcodetreebinary-treerecursionstringparsing
LeetCode 585: Investments in 2016

A clear SQL guide for summing 2016 investments for policies with repeated 2015 investment values and unique locations.

leetcodesqlgroup-byhavingaggregation
LeetCode 560: Subarray Sum Equals K

A clear explanation of Subarray Sum Equals K using prefix sums and a hash map to count matching subarrays in linear time.

leetcodearrayhash-mapprefix-sum
LeetCode 535: Encode and Decode TinyURL

A clear explanation of designing a simple URL encoder and decoder using a hash map and generated keys.

leetcodedesignhash-tablestring
LeetCode 534: Game Play Analysis III

A clear explanation of computing cumulative games played per player and date using SQL window functions.

leetcodesqldatabasewindow-functionaggregation
LeetCode 584: Find Customer Referee

A clear SQL guide for selecting customers who were not referred by customer 2, including customers with no referee.

leetcodesqlwherenull
LeetCode 559: Maximum Depth of N-ary Tree

A clear explanation of Maximum Depth of N-ary Tree using recursive depth-first search.

leetcodetreedepth-first-searchbreadth-first-search
LeetCode 583: Delete Operation for Two Strings

A clear dynamic programming solution for finding the minimum deletions needed to make two strings equal.

leetcodedynamic-programmingstringlcs
LeetCode 558: Logical OR of Two Binary Grids Represented as Quad-Trees

A clear explanation of merging two quad-trees using recursive logical OR operations.

leetcodetreequad-treerecursion
LeetCode 533: Lonely Pixel II

A clear explanation of counting black lonely pixels using row counts, column counts, and duplicate row patterns.

leetcodearraymatrixhash-tablecounting
LeetCode 557: Reverse Words in a String III

A clear explanation of Reverse Words in a String III using two-pointer scanning and string reversal.

leetcodestringtwo-pointers