brain

tamnd's digital brain — notes, problems, research

43815 notes

LeetCode 854: K-Similar Strings

A clear explanation of finding the minimum number of swaps needed to transform one anagram string into another using BFS.

leetcodestringbfsgraphshortest-path
LeetCode 754: Reach a Number

A clear explanation of reaching a target on a number line using cumulative sums and parity.

leetcodemathgreedynumber-line
LeetCode 729: My Calendar I

Implement a calendar that accepts a booking only when it does not overlap with any existing booking.

leetcodedesignarrayintervals
LeetCode 703: Kth Largest Element in a Stream

A clear explanation of maintaining the kth largest element in a stream using a fixed-size min heap.

leetcodeheappriority-queuedesigndata-stream
LeetCode 679: 24 Game

Determine whether four numbers can be combined with arithmetic operations and parentheses to produce 24.

leetcodearraymathbacktracking
LeetCode 928: Minimize Malware Spread II

A clear explanation of solving Minimize Malware Spread II by removing each infected node and simulating the final malware spread.

leetcodegraphbreadth-first-searchdepth-first-searchsimulation
LeetCode 978: Longest Turbulent Subarray

A clear explanation of finding the longest subarray whose adjacent comparisons alternate between greater-than and less-than.

leetcodearraydynamic-programmingsliding-window
LeetCode 878: Nth Magical Number

A clear explanation of finding the nth magical number using binary search, greatest common divisor, least common multiple, and inclusion-exclusion.

leetcodemathbinary-searchnumber-theory
LeetCode 953: Verifying an Alien Dictionary

A clear explanation of checking whether words are sorted according to a custom alien alphabet order.

leetcodestringsortinghash-table
LeetCode 828: Count Unique Characters of All Substrings of a Given String

A clear explanation of Count Unique Characters of All Substrings using contribution counting with previous and next occurrences.

leetcodestringhash-mapdynamic-programmingcombinatorics
LeetCode 903: Valid Permutations for DI Sequence

A clear explanation of counting valid DI permutations using dynamic programming and prefix sums.

leetcodedynamic-programmingprefix-sumpermutation
LeetCode 661: Image Smoother

A clear explanation of averaging neighboring pixels in a matrix using direct simulation.

leetcodematrixsimulationarray
LeetCode 660: Remove 9

A clear explanation of finding the nth positive integer that does not contain the digit 9 using base-9 conversion.

leetcodemathbase-conversion
LeetCode 575: Distribute Candies

A clear explanation of Distribute Candies using a set to count candy types and a simple limit argument.

leetcodearrayhash-setgreedy
LeetCode 523: Continuous Subarray Sum

A clear explanation of detecting a subarray whose sum is a multiple of k using prefix sums and modular arithmetic.

leetcodearrayhash-mapprefix-summath
LeetCode 778: Swim in Rising Water

A clear explanation of finding the minimum time to reach the bottom-right cell using a priority queue and minimax path reasoning.

leetcodegraphheappriority-queuedijkstramatrix
LeetCode 611: Valid Triangle Number

A two-pointer guide for counting triplets that can form valid triangles after sorting the side lengths.

leetcodearraysortingtwo-pointersbinary-search
LeetCode 659: Split Array into Consecutive Subsequences

A clear explanation of deciding whether a sorted array can be split into consecutive subsequences of length at least three.

leetcodearrayhash-mapgreedy
LeetCode 610: Triangle Judgement

A SQL guide for checking whether three side lengths can form a valid triangle using the triangle inequality.

leetcodesqlcase-whenmathdatabase
LeetCode 550: Game Play Analysis IV

A clear explanation of calculating the fraction of players who logged in again the day after their first login.

leetcodesqldatabaseaggregationjoin
LeetCode 599: Minimum Index Sum of Two Lists

A clear hash map solution for finding common strings with the smallest index sum.

leetcodearrayhash-mapstring
LeetCode 658: Find K Closest Elements

A clear explanation of finding the k closest elements to a target using binary search and a sliding window.

leetcodearraybinary-searchtwo-pointerssliding-window
LeetCode 609: Find Duplicate File in System

A hash map guide for grouping file paths by identical file content and returning only duplicate groups.

leetcodearrayhash-tablestringparsing
LeetCode 549: Binary Tree Longest Consecutive Sequence II

A clear explanation of finding the longest increasing or decreasing consecutive path in a binary tree using DFS.

leetcodetreebinary-treedepth-first-searchdynamic-programming
LeetCode 608: Tree Node

A SQL guide for classifying binary tree nodes as Root, Inner, or Leaf based on parent-child relationships.

leetcodesqlcase-whenself-referencedatabase
LeetCode 657: Robot Return to Origin

A clear explanation of determining whether a robot returns to the origin after executing movement instructions.

leetcodesimulationstring
LeetCode 598: Range Addition II

A clear math solution for counting the maximum values after repeated top-left matrix increment operations.

leetcodearraymathmatrix
LeetCode 574: Winning Candidate

A clear explanation of Winning Candidate using SQL aggregation to count votes and return the candidate with the most votes.

leetcodesqldatabasegroup-byjoin
LeetCode 548: Split Array with Equal Sum

A clear explanation of splitting an array into four equal-sum parts using prefix sums and set-based search.

leetcodearrayprefix-sumhash-set
LeetCode 522: Longest Uncommon Subsequence II

A clear explanation of finding the longest uncommon subsequence among many strings using subsequence checks.

leetcodearraystringtwo-pointerssorting
LeetCode 521: Longest Uncommon Subsequence I

A clear explanation of finding the longest uncommon subsequence between two strings using simple case analysis.

leetcodestring
LeetCode 607: Sales Person

A SQL guide for finding salespeople who never had an order related to the company named RED.

leetcodesqlanti-joinnot-existsdatabase
LeetCode 630: Course Schedule III

A greedy heap solution for taking the maximum number of courses before their deadlines.

leetcodearraygreedyheapsorting
LeetCode 656: Coin Path

A clear explanation of finding the minimum-cost path with bounded jumps, blocked cells, and lexicographic tie-breaking.

leetcodedynamic-programmingpatharray
LeetCode 606: Construct String from Binary Tree

A recursive guide for converting a binary tree into a preorder parenthesized string while preserving the one-to-one mapping between the tree and the string.

leetcodetreebinary-treerecursionstring
LeetCode 547: Number of Provinces

A clear explanation of counting connected components in an undirected graph represented by an adjacency matrix.

leetcodegraphdepth-first-searchbreadth-first-searchunion-find
LeetCode 597: Friend Requests I: Overall Acceptance Rate

A clear SQL guide for computing the overall friend request acceptance rate with duplicate pairs counted once.

leetcodesqlaggregationdistinctround
LeetCode 573: Squirrel Simulation

A clear explanation of Squirrel Simulation using Manhattan distance and the special first trip.

leetcodemathgreedysimulation
LeetCode 520: Detect Capital

A clear explanation of checking whether a word uses capital letters correctly by counting uppercase letters.

leetcodestring
LeetCode 572: Subtree of Another Tree

A clear explanation of Subtree of Another Tree using recursive tree matching and DFS.

leetcodetreebinary-treedepth-first-searchrecursion
LeetCode 519: Random Flip Matrix

A clear explanation of randomly flipping zero cells in a matrix without repetition using hash mapping and virtual swapping.

leetcoderandomizationhash-mapmatrix
LeetCode 596: Classes With at Least 5 Students

A clear SQL guide for finding classes that have at least five students.

leetcodesqlgroup-byhavingaggregation
LeetCode 571: Find Median Given Frequency of Numbers

A clear explanation of Find Median Given Frequency of Numbers using cumulative frequency and SQL window functions.

leetcodesqldatabasewindow-functionprefix-sum
LeetCode 518: Coin Change II

A clear explanation of counting coin-change combinations using dynamic programming.

leetcodedynamic-programmingarraycoin-changeunbounded-knapsack
LeetCode 546: Remove Boxes

A clear explanation of maximizing remove-box scores using interval dynamic programming with memoization.

leetcodedynamic-programminginterval-dpmemoizationrecursion
LeetCode 570: Managers with at Least 5 Direct Reports

A clear explanation of Managers with at Least 5 Direct Reports using grouping and a self join.

leetcodesqldatabasegroup-byself-join
LeetCode 545: Boundary of Binary Tree

A clear explanation of collecting the boundary of a binary tree using separate left boundary, leaves, and right boundary traversals.

leetcodetreebinary-treedepth-first-search
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