brain

tamnd's digital brain — notes, problems, research

42768 notes

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
LeetCode 708: Insert into a Sorted Circular Linked List

A clear explanation of inserting a value into a sorted circular linked list while preserving the circular sorted order.

leetcodelinked-listcircular-linked-list
LeetCode 809: Expressive Words

A two-pointer group comparison solution for counting how many words can be stretched to match a target string.

leetcodestringtwo-pointers
LeetCode 859: Buddy Strings

A clear explanation of checking whether one swap in a string can make it equal to another string.

leetcodestringhash-table
LeetCode 759: Employee Free Time

A clear explanation of finding common free time by merging all employee busy intervals and returning the gaps.

leetcodearraysortingintervalsmerge-intervals
LeetCode 734: Sentence Similarity

Check whether two word arrays are sentence-similar using a hash set of symmetric similar word pairs.

leetcodearraystringhash-table
LeetCode 883: Projection Area of 3D Shapes

A clear explanation of computing the projection areas of stacked cubes from top, front, and side views.

leetcodematrixgeometrysimulation
LeetCode 933: Number of Recent Calls

A clear explanation of solving Number of Recent Calls using a queue as a sliding time window.

leetcodedesignqueuedata-streamsliding-window
LeetCode 983: Minimum Cost For Tickets

A clear explanation of finding the cheapest way to cover all travel days using dynamic programming.

leetcodearraydynamic-programming
LeetCode 908: Smallest Range I

A clear explanation of minimizing an array score after each value can move by at most k.

leetcodearraymath
LeetCode 956: Tallest Billboard

A clear explanation of solving Tallest Billboard using dynamic programming over height differences.

leetcodearraydynamic-programmingknapsack
LeetCode 783: Minimum Distance Between BST Nodes

A clear explanation of finding the minimum difference between any two nodes in a BST using inorder traversal.

leetcodetreebinary-search-treedfsinorder-traversal
LeetCode 832: Flipping an Image

A clear explanation of the Flipping an Image problem using row reversal, bit inversion, and an in-place two-pointer method.

leetcodearraymatrixtwo-pointersbit-manipulationsimulation
LeetCode 689: Maximum Sum of 3 Non-Overlapping Subarrays

Find three non-overlapping subarrays of length k with maximum total sum and return the lexicographically smallest starting indices.

leetcodearraydynamic-programmingsliding-window
LeetCode 670: Maximum Swap

A clear explanation of maximizing an integer by swapping at most two digits once.

leetcodegreedymathstring
LeetCode 641: Design Circular Deque

An array-based circular buffer solution for implementing a fixed-size double-ended queue.

leetcodedesignarrayqueuecircular-buffer
LeetCode 640: Solve the Equation

A string parsing solution for reducing a linear equation into coefficient and constant terms.

leetcodestringmathsimulationparsing
LeetCode 669: Trim a Binary Search Tree

A clear explanation of trimming a BST so that all remaining node values lie inside a given inclusive range.

leetcodetreebinary-treebinary-search-treedepth-first-searchrecursion
LeetCode 688: Knight Probability in Chessboard

Compute the probability that a knight remains on an n x n chessboard after exactly k random moves using dynamic programming.

leetcodedynamic-programmingprobabilitychessboard
LeetCode 639: Decode Ways II

A dynamic programming solution for counting decodings of a digit string with wildcard characters.

leetcodedynamic-programmingstringmodulo
LeetCode 687: Longest Univalue Path

Find the longest path in a binary tree where every node on the path has the same value using depth-first search.

leetcodetreebinary-treedfsrecursion
LeetCode 620: Not Boring Movies

A SQL guide for filtering movies with odd IDs and non-boring descriptions, then sorting by rating.

leetcodesqlfilteringorder-by
LeetCode 707: Design Linked List

A clear explanation of implementing a linked list from scratch using nodes, a dummy head, and a size counter.

leetcodelinked-listdesignsingly-linked-list
LeetCode 758: Bold Words in String

A clear explanation of marking matching substrings and merging overlapping bold ranges.

leetcodestringintervalssimulation
LeetCode 858: Mirror Reflection

A clear explanation of Mirror Reflection using room unfolding, least common multiples, and parity.

leetcodemathgeometrynumber-theory
LeetCode 808: Soup Servings

A probability dynamic programming solution for computing whether soup A empties before soup B, with an early return for large input.

leetcodedynamic-programmingprobabilitymemoization
LeetCode 932: Beautiful Array

A clear explanation of solving Beautiful Array using divide and conquer with odd and even transformations.

leetcodearraymathdivide-and-conquer
LeetCode 733: Flood Fill

Recolor the connected component containing the starting pixel using depth-first search.

leetcodearraymatrixdepth-first-searchbreadth-first-search
LeetCode 982: Triples with Bitwise AND Equal To Zero

A clear explanation of counting ordered triples whose bitwise AND is zero using pairwise AND counts.

leetcodearrayhash-tablebit-manipulationcounting
LeetCode 668: Kth Smallest Number in Multiplication Table

A clear explanation of finding the kth smallest value in an m by n multiplication table using binary search on answer.

leetcodebinary-searchmath
LeetCode 638: Shopping Offers

A DFS and memoization solution for finding the minimum cost to satisfy item needs using individual prices and reusable special offers.

leetcodedynamic-programmingdfsmemoizationbacktracking
LeetCode 619: Biggest Single Number

A SQL guide for finding the largest number that appears exactly once in a table.

leetcodesqlgroup-byhavingaggregation
LeetCode 686: Repeated String Match

Find the minimum number of times one string must be repeated so another string becomes a substring.

leetcodestringstring-matching
LeetCode 685: Redundant Connection II

Find the directed edge to remove so a graph becomes a rooted tree again, handling both cycles and nodes with two parents.

leetcodegraphunion-finddirected-graph
LeetCode 637: Average of Levels in Binary Tree

A breadth-first search solution for computing the average value of nodes at each level of a binary tree.

leetcodetreebinary-treebreadth-first-searchqueue
LeetCode 667: Beautiful Arrangement II

A clear explanation of constructing an array with exactly k distinct adjacent differences using a greedy pattern.

leetcodearraygreedyconstructive-algorithm
LeetCode 618: Students Report By Geography

A SQL guide for pivoting rows into columns using ranking and conditional aggregation.

leetcodesqlpivotrow-numbergroup-by
LeetCode 636: Exclusive Time of Functions

A stack-based solution for computing exclusive execution time from nested start and end logs.

leetcodestacksimulationstring
LeetCode 617: Merge Two Binary Trees

A recursive tree traversal guide for merging two binary trees node by node.

leetcodetreebinary-treerecursiondfs
LeetCode 882: Reachable Nodes In Subdivided Graph

A clear explanation of counting reachable original and subdivided nodes using Dijkstra's shortest path algorithm.

leetcodegraphdijkstrashortest-pathheap
LeetCode 907: Sum of Subarray Minimums

A clear explanation of summing subarray minimums using a monotonic stack and contribution counting.

leetcodearraystackmonotonic-stack
LeetCode 782: Transform to Chessboard

A clear explanation of transforming a binary board into a chessboard using feasibility checks and minimum row and column swaps.

leetcodematrixmathbit-manipulationgreedy
LeetCode 831: Masking Personal Information

A clear explanation of the Masking Personal Information problem using string parsing and format-specific masking rules.

leetcodestringsimulation
LeetCode 684: Redundant Connection

Find the extra edge in an undirected graph that creates a cycle using Union-Find.

leetcodegraphunion-finddisjoint-set
LeetCode 666: Path Sum IV

A clear explanation of computing all root-to-leaf path sums from a compact three-digit binary tree encoding.

leetcodetreebinary-treedepth-first-searchhash-map
LeetCode 616: Add Bold Tag in String

A string-marking guide for adding bold tags around all matched words while merging overlapping and adjacent bold regions.

leetcodestringhash-tablestring-matching
LeetCode 635: Design Log Storage System

A design solution for storing timestamped logs and retrieving IDs by inclusive time range at a chosen granularity.

leetcodedesignstringarraytimestamp
LeetCode 757: Set Intersection Size At Least Two

A clear explanation of solving interval intersection constraints using greedy sorting and minimal point selection.

leetcodegreedysortingintervals