brain

tamnd's digital brain — notes, problems, research

43815 notes

LeetCode 766: Toeplitz Matrix

A clear explanation of checking whether every top-left to bottom-right diagonal in a matrix has the same value.

leetcodearraymatrix
LeetCode 742: Closest Leaf in a Binary Tree

Find the nearest leaf to a target node by converting the tree into an undirected graph and running breadth-first search.

leetcodetreegraphbreadth-first-searchdepth-first-search
LeetCode 790: Domino and Tromino Tiling

A clear explanation of counting tilings of a 2 x n board using dominoes and L-shaped trominoes with dynamic programming.

leetcodedynamic-programmingmathtiling
LeetCode 715: Range Module

A clear explanation of designing a range module that can add, query, and remove half-open intervals.

leetcodedesignintervalordered-listbinary-search
LeetCode 621: Task Scheduler

A clear explanation of Task Scheduler using frequency counting and the greedy block formula.

leetcodearrayhash-mapcountinggreedy
LeetCode 939: Minimum Area Rectangle

A clear explanation of solving Minimum Area Rectangle using diagonal point pairs and constant-time point lookup.

leetcodearrayhash-setgeometry
LeetCode 889: Construct Binary Tree from Preorder and Postorder Traversal

A clear explanation of reconstructing a binary tree from preorder and postorder traversals using recursion and index ranges.

leetcodearrayhash-tabletreebinary-treedivide-and-conquer
LeetCode 741: Cherry Pickup

Maximize cherries collected on a round trip by converting the problem into two simultaneous forward paths and solving with dynamic programming.

leetcodearraymatrixdynamic-programming
LeetCode 714: Best Time to Buy and Sell Stock with Transaction Fee

A clear explanation of maximizing stock trading profit with unlimited transactions and a fixed transaction fee using dynamic programming.

leetcodearraydynamic-programminggreedystock
LeetCode 962: Maximum Width Ramp

A clear explanation of finding the maximum width ramp using a monotonic decreasing stack.

leetcodearraymonotonic-stack
LeetCode 838: Push Dominoes

A clear explanation of the Push Dominoes problem using force propagation and a two-pass scan.

leetcodestringtwo-pointersdynamic-programming
LeetCode 765: Couples Holding Hands

A clear explanation of minimizing swaps so every couple sits together using greedy position tracking.

leetcodegreedyarrayhash-table
LeetCode 740: Delete and Earn

Transform the problem into House Robber dynamic programming by grouping equal values into total points.

leetcodearraydynamic-programminghash-table
LeetCode 789: Escape The Ghosts

A clear explanation of deciding whether escape is possible by comparing Manhattan distances to the target.

leetcodemathgeometrymanhattan-distance
LeetCode 914: X of a Kind in a Deck of Cards

A clear explanation of checking whether card counts share a common group size using the greatest common divisor.

leetcodearrayhash-tablemathnumber-theorygcd
LeetCode 988: Smallest String Starting From Leaf

A clear explanation of finding the lexicographically smallest leaf-to-root string in a binary tree using DFS.

leetcodetreebinary-treedepth-first-searchstring
LeetCode 864: Shortest Path to Get All Keys

A clear explanation of finding the minimum moves to collect all keys in a grid using BFS with key bitmasks.

leetcodebfsbitmaskmatrixshortest-path
LeetCode 814: Binary Tree Pruning

A postorder DFS solution for removing every binary tree subtree that does not contain a 1.

leetcodebinary-treedfsrecursion
LeetCode 961: N-Repeated Element in Size 2N Array

A clear explanation of finding the element repeated N times using a hash set.

leetcodearrayhash-table
LeetCode 888: Fair Candy Swap

A clear explanation of finding one candy box swap that makes Alice and Bob have equal total candies.

leetcodearrayhash-tablemath
LeetCode 938: Range Sum of BST

A clear explanation of solving Range Sum of BST using DFS with binary search tree pruning.

leetcodetreebinary-search-treedepth-first-search
LeetCode 788: Rotated Digits

A clear explanation of counting good numbers after rotating every digit by 180 degrees.

leetcodemathstringdigit-dp
LeetCode 739: Daily Temperatures

Find how many days each temperature must wait for a warmer future day using a monotonic stack.

leetcodearraystackmonotonic-stack
LeetCode 713: Subarray Product Less Than K

A clear explanation of counting contiguous subarrays whose product is less than k using a sliding window.

leetcodearraysliding-windowtwo-pointers
LeetCode 764: Largest Plus Sign

A clear explanation of finding the largest plus sign in a mined grid using four directional dynamic programming scans.

leetcodedynamic-programmingmatrixgrid
LeetCode 837: New 21 Game

A clear explanation of the New 21 Game problem using probability dynamic programming and a sliding window sum.

leetcodemathdynamic-programmingsliding-windowprobability
LeetCode 700: Search in a Binary Search Tree

Search for a target value in a binary search tree and return the subtree rooted at the matching node.

leetcodetreebinary-search-treedfs
LeetCode 913: Cat and Mouse

A clear explanation of Cat and Mouse using game states, reverse BFS, and topological propagation.

leetcodegraphgame-theorybreadth-first-searchtopological-sort
LeetCode 699: Falling Squares

Simulate falling squares on a number line and track the maximum stack height after each placement.

leetcodeintervalssimulationsegment-treecoordinate-compression
LeetCode 813: Largest Sum of Averages

A dynamic programming and prefix sum solution for partitioning an array into adjacent groups with maximum total average.

leetcodedynamic-programmingprefix-sumarray
LeetCode 987: Vertical Order Traversal of a Binary Tree

A clear explanation of vertical tree traversal using coordinates, DFS, sorting, and column grouping.

leetcodetreebinary-treedepth-first-searchbreadth-first-searchsorting
LeetCode 863: All Nodes Distance K in Binary Tree

A clear explanation of finding all binary tree nodes at distance k from a target node by treating the tree as an undirected graph.

leetcodebinary-treegraphdfsbfs
LeetCode 712: Minimum ASCII Delete Sum for Two Strings

A clear explanation of using dynamic programming to minimize the ASCII cost of deletions needed to make two strings equal.

leetcodestringdynamic-programming
LeetCode 960: Delete Columns to Make Sorted III

A clear explanation of deleting the minimum number of columns so every remaining row is individually sorted.

leetcodestringdynamic-programminglongest-increasing-subsequence
LeetCode 738: Monotone Increasing Digits

Find the largest number less than or equal to n whose digits are monotone increasing using a greedy digit adjustment.

leetcodemathgreedy
LeetCode 763: Partition Labels

A clear explanation of partitioning a string into the maximum number of parts so each character appears in at most one part.

leetcodestringgreedyhash-tabletwo-pointers
LeetCode 887: Super Egg Drop

A clear explanation of finding the minimum worst-case number of moves using dynamic programming over eggs and moves.

leetcodedynamic-programmingmathbinary-search
LeetCode 937: Reorder Data in Log Files

A clear explanation of solving Reorder Data in Log Files using custom sorting and stable handling of digit logs.

leetcodestringsortingcustom-sort
LeetCode 836: Rectangle Overlap

A clear explanation of the Rectangle Overlap problem using axis projections and positive intersection area.

leetcodemathgeometry
LeetCode 787: Cheapest Flights Within K Stops

A clear explanation of finding the cheapest flight route with at most k stops using bounded Bellman-Ford relaxation.

leetcodegraphdynamic-programmingbellman-fordshortest-path
LeetCode 912: Sort an Array

A clear explanation of sorting an array without built-in sorting using merge sort.

leetcodearraysortingmerge-sortdivide-and-conquer
LeetCode 650: 2 Keys Keyboard

A dynamic programming and prime factorization solution for finding the minimum operations needed to produce n characters.

leetcodedynamic-programmingmathprime-factorization
LeetCode 698: Partition to K Equal Sum Subsets

Decide whether an array can be divided into k non-empty subsets with equal sums using backtracking and pruning.

leetcodearraybacktrackingdynamic-programmingbitmask
LeetCode 649: Dota2 Senate

A queue-based simulation for predicting which party wins after senators ban opponents in turn order.

leetcodestringqueuegreedysimulation
LeetCode 711: Number of Distinct Islands II

A clear explanation of counting distinct island shapes under rotation and reflection using normalization and geometric transformations.

leetcodegriddfsgeometryhashing
LeetCode 986: Interval List Intersections

A clear explanation of finding intersections between two sorted disjoint interval lists using two pointers.

leetcodearraytwo-pointersintervals
LeetCode 812: Largest Triangle Area

A geometry solution for finding the largest triangle area by checking every triplet of points with the cross product formula.

leetcodemathgeometryarray
LeetCode 862: Shortest Subarray with Sum at Least K

A clear explanation of finding the shortest non-empty subarray with sum at least k using prefix sums and a monotonic deque.

leetcodearrayprefix-summonotonic-queuedeque
LeetCode 762: Prime Number of Set Bits in Binary Representation

A clear explanation of counting numbers whose binary representation has a prime number of set bits.

leetcodebit-manipulationmath
LeetCode 936: Stamping The Sequence

A clear explanation of solving Stamping The Sequence using reverse simulation and BFS-style processing.

leetcodegreedyqueuestringgraph
LeetCode 959: Regions Cut By Slashes

A clear explanation of counting regions formed by slashes using union find over four triangles per cell.

leetcodematrixunion-findgraph
LeetCode 886: Possible Bipartition

A clear explanation of checking whether people can be split into two groups using graph coloring and bipartite graph detection.

leetcodegraphdfsbfsbipartite-graph
LeetCode 737: Sentence Similarity II

Check sentence similarity with transitive word relationships using union-find.

leetcodearraystringhash-tableunion-findgraph
LeetCode 835: Image Overlap

A clear explanation of the Image Overlap problem using translation vectors and frequency counting.

leetcodearraymatrixhash-map
LeetCode 786: K-th Smallest Prime Fraction

A clear explanation of finding the kth smallest fraction from a sorted array using a min-heap.

leetcodearrayheappriority-queuesorting
LeetCode 911: Online Election

A clear explanation of Online Election using preprocessing and binary search over vote times.

leetcodedesignarrayhash-tablebinary-search
LeetCode 697: Degree of an Array

Find the shortest contiguous subarray with the same degree as the whole array using frequency counts and first occurrence indices.

leetcodearrayhash-mapcounting
LeetCode 648: Replace Words

A trie-based solution for replacing each derivative word with the shortest matching root.

leetcodetriestringhash-table
LeetCode 696: Count Binary Substrings

Count substrings with equal consecutive groups of 0s and 1s using run lengths.

leetcodestringcounting
LeetCode 647: Palindromic Substrings

A center expansion solution for counting every palindromic substring in a string.

leetcodestringtwo-pointerspalindrome
LeetCode 695: Max Area of Island

Find the largest connected island area in a binary grid using depth-first search.

leetcodearraymatrixdfsbfs
LeetCode 710: Random Pick with Blacklist

A clear explanation of selecting a uniformly random integer while excluding blacklisted values using remapping and hashing.

leetcodehash-tablerandommathdesign
LeetCode 861: Score After Flipping Matrix

A clear explanation of maximizing a binary matrix score using greedy row and column flips.

leetcodematrixgreedybit-manipulation
LeetCode 675: Cut Off Trees for Golf Event

A clear explanation of cutting trees in increasing height order using repeated BFS on a grid.

leetcodearraymatrixbreadth-first-searchsorting
LeetCode 811: Subdomain Visit Count

A hash map solution for accumulating visit counts across domains and all of their parent subdomains.

leetcodehash-mapstringcounting
LeetCode 646: Maximum Length of Pair Chain

A greedy interval scheduling solution for finding the longest chain of valid pairs.

leetcodegreedysortingdynamic-programming
LeetCode 694: Number of Distinct Islands

Count unique island shapes in a binary grid using DFS and relative coordinates.

leetcodearraymatrixdfshash-set
LeetCode 761: Special Binary String

A clear explanation of making a special binary string lexicographically largest using recursive decomposition and sorting.

leetcodestringrecursiondivide-and-conquersorting
LeetCode 736: Parse Lisp Expression

Evaluate a Lisp-like expression with integers, variables, let bindings, addition, multiplication, and lexical scope.

leetcodestringrecursionhash-tableparsing
LeetCode 935: Knight Dialer

A clear explanation of solving Knight Dialer using dynamic programming over the phone keypad graph.

leetcodedynamic-programminggraphmatrix
LeetCode 985: Sum of Even Numbers After Queries

A clear explanation of maintaining the sum of even numbers after each array update.

leetcodearraysimulation
LeetCode 885: Spiral Matrix III

A clear explanation of generating grid coordinates in an outward clockwise spiral using simulation.

leetcodearraymatrixsimulation
LeetCode 958: Check Completeness of a Binary Tree

A clear explanation of checking whether a binary tree is complete using level-order traversal.

leetcodetreebinary-treebreadth-first-searchqueue
LeetCode 910: Smallest Range II

A clear explanation of minimizing the array range after adding either +k or -k to every element.

leetcodearraygreedysorting
LeetCode 785: Is Graph Bipartite?

A clear explanation of checking whether an undirected graph can be split into two independent sets using graph coloring.

leetcodegraphdfsbfscoloring
LeetCode 834: Sum of Distances in Tree

A clear explanation of the Sum of Distances in Tree problem using tree DP, subtree sizes, and rerooting.

leetcodetreegraphdepth-first-searchdynamic-programmingrerooting
LeetCode 709: To Lower Case

A clear explanation of converting uppercase ASCII letters to lowercase by scanning the string once.

leetcodestringascii
LeetCode 810: Chalkboard XOR Game

A math and bit manipulation solution for deciding whether Alice wins the XOR removal game.

leetcodemathbit-manipulationgame-theory
LeetCode 860: Lemonade Change

A clear explanation of Lemonade Change using greedy simulation and bill counting.

leetcodearraygreedysimulation
LeetCode 645: Set Mismatch

A counting and math solution for finding the duplicated number and the missing number in a corrupted set.

leetcodearrayhash-tablemathsorting
LeetCode 674: Longest Continuous Increasing Subsequence

A clear explanation of finding the longest strictly increasing contiguous subarray using a single scan.

leetcodearraydynamic-programmingsliding-window
LeetCode 693: Binary Number with Alternating Bits

Check whether every adjacent bit in a positive integer's binary representation is different.

leetcodebit-manipulation
LeetCode 760: Find Anagram Mappings

A clear explanation of mapping each element in one array to a matching index in its anagram using a hash map.

leetcodearrayhash-table
LeetCode 735: Asteroid Collision

Simulate asteroid collisions using a stack that keeps the surviving asteroids in order.

leetcodearraystacksimulation
LeetCode 934: Shortest Bridge

A clear explanation of solving Shortest Bridge using DFS to mark one island and BFS to expand toward the other island.

leetcodematrixgraphdepth-first-searchbreadth-first-search
LeetCode 884: Uncommon Words from Two Sentences

A clear explanation of finding uncommon words by counting word frequencies across both sentences.

leetcodehash-tablestringcounting
LeetCode 984: String Without AAA or BBB

A clear explanation of constructing a string with exact counts of a and b while avoiding three equal consecutive characters.

leetcodestringgreedy
LeetCode 957: Prison Cells After N Days

A clear explanation of simulating prison cell transitions efficiently using cycle detection.

leetcodearrayhash-tablesimulationcycle-detection
LeetCode 644: Maximum Average Subarray II

A binary search solution for finding the maximum average of any contiguous subarray with length at least k.

leetcodearraybinary-searchprefix-sum
LeetCode 692: Top K Frequent Words

Find the k most frequent words using frequency counting and custom sorting by count and lexicographical order.

leetcodehash-mapsortingheapstring
LeetCode 673: Number of Longest Increasing Subsequence

A clear explanation of counting how many longest strictly increasing subsequences exist using dynamic programming.

leetcodearraydynamic-programminglongest-increasing-subsequence
LeetCode 672: Bulb Switcher II

A clear explanation of counting possible bulb states after pressing four toggle buttons exactly presses times.

leetcodemathbit-manipulationstate-compression
LeetCode 691: Stickers to Spell Word

Find the minimum number of stickers needed to form a target string using top-down dynamic programming with memoization.

leetcodedynamic-programmingmemoizationbitmaskstring
LeetCode 643: Maximum Average Subarray I

A sliding window solution for finding the maximum average among all contiguous subarrays of fixed length k.

leetcodearraysliding-window
LeetCode 909: Snakes and Ladders

A clear explanation of Snakes and Ladders using breadth-first search over board squares.

leetcodegraphbreadth-first-searchmatrix
LeetCode 690: Employee Importance

Compute the total importance of an employee and all direct and indirect subordinates using a hash map and depth-first search.

leetcodehash-mapdfsbfstree
LeetCode 642: Design Search Autocomplete System

A trie-based design for returning the top three historical sentences for a typed prefix.

leetcodedesigntriehash-mapsorting
LeetCode 784: Letter Case Permutation

A clear explanation of generating all strings formed by independently changing each letter to lowercase or uppercase.

leetcodestringbacktrackingdfsrecursion
LeetCode 833: Find And Replace in String

A clear explanation of the Find And Replace in String problem using simultaneous replacement, source matching, and a replacement map.

leetcodestringarrayhash-mapsimulation
LeetCode 671: Second Minimum Node In a Binary Tree

A clear explanation of finding the second minimum value in a special binary tree using DFS.

leetcodetreebinary-treedepth-first-search