brain

tamnd's digital brain — notes, problems, research

43815 notes

LeetCode 397: Integer Replacement

A clear explanation of reducing an integer to 1 with the fewest operations using greedy bit decisions.

leetcodemathbit-manipulationgreedyrecursion
LeetCode 372: Super Pow

A clear explanation of computing large modular exponentiation using fast power, modular arithmetic, and digit decomposition.

leetcodemathmodular-arithmeticrecursionfast-power
LeetCode 325: Maximum Size Subarray Sum Equals k

A clear explanation of Maximum Size Subarray Sum Equals k using prefix sums and earliest-index hashing.

leetcodearrayhash-tableprefix-sum
LeetCode 324: Wiggle Sort II

A clear explanation of Wiggle Sort II using sorting, median splitting, and virtual indexing.

leetcodearraysortinggreedyquickselect
LeetCode 323: Number of Connected Components in an Undirected Graph

A clear explanation of counting connected components using Union-Find and graph traversal.

leetcodegraphunion-finddfsbfs
LeetCode 396: Rotate Function

A clear explanation of maximizing the rotation function using a recurrence instead of simulating every rotation.

leetcodearraymathdynamic-programming
LeetCode 469: Convex Polygon

A clear explanation of checking whether ordered points form a convex polygon using cross products.

leetcodearraymathgeometry
LeetCode 447: Number of Boomerangs

Count ordered boomerang tuples by fixing each point as the center and grouping other points by squared distance.

leetcodearrayhash-tablemathgeometry
LeetCode 497: Random Point in Non-overlapping Rectangles

A clear explanation of uniformly picking an integer point from non-overlapping rectangles using prefix sums and binary search.

leetcoderandomizedbinary-searchprefix-sumgeometry
LeetCode 420: Strong Password Checker

A clear explanation of checking the minimum edits needed to make a password strong using greedy handling of length, missing character types, and repeated runs.

leetcodestringgreedy
LeetCode 371: Sum of Two Integers

A clear explanation of adding two integers without using plus or minus by using XOR, AND, carry, and a 32-bit mask.

leetcodebit-manipulationmath
LeetCode 350: Intersection of Two Arrays II

A clear explanation of Intersection of Two Arrays II using frequency counting.

leetcodearrayhash-tabletwo-pointerssorting
LeetCode 322: Coin Change

A clear explanation of Coin Change using dynamic programming for minimum coin count.

leetcodedynamic-programmingarray
LeetCode 496: Next Greater Element I

A clear explanation of finding the next greater element using a monotonic decreasing stack and hash map.

leetcodearraystackmonotonic-stackhash-table
LeetCode 419: Battleships in a Board

A clear explanation of counting battleships in a board using one-pass observation without modifying the grid.

leetcodematrixarraycounting
LeetCode 321: Create Maximum Number

A clear explanation of Create Maximum Number using monotonic stacks for subsequences and greedy merging.

leetcodearraygreedymonotonic-stacktwo-pointers
LeetCode 495: Teemo Attacking

A clear explanation of calculating total poisoned duration by merging overlapping attack intervals.

leetcodearrayintervalssimulation
LeetCode 468: Validate IP Address

A clear explanation of validating IPv4 and IPv6 addresses by checking segment count, length, characters, range, and leading-zero rules.

leetcodestringsimulation
LeetCode 446: Arithmetic Slices II - Subsequence

Count arithmetic subsequences of length at least three using dynamic programming with one hash map per ending index.

leetcodearraydynamic-programminghash-table
LeetCode 418: Sentence Screen Fitting

A clear explanation of fitting a sentence onto a screen using cyclic string simulation and greedy row transitions.

leetcodestringgreedysimulationdynamic-programming
LeetCode 395: Longest Substring with At Least K Repeating Characters

A clear explanation of finding the longest substring where every character appears at least k times using divide and conquer.

leetcodestringhash-tabledivide-and-conquersliding-window
LeetCode 370: Range Addition

A clear explanation of applying many range updates efficiently using a difference array and prefix sums.

leetcodearrayprefix-sumdifference-array
LeetCode 349: Intersection of Two Arrays

A clear explanation of Intersection of Two Arrays using hash sets for uniqueness and fast lookup.

leetcodearrayhash-tabletwo-pointerssorting
LeetCode 320: Generalized Abbreviation

A clear explanation of Generalized Abbreviation using backtracking to choose whether each character is kept or abbreviated.

leetcodestringbacktrackingbit-manipulation
LeetCode 300: Longest Increasing Subsequence

A dynamic programming and patience sorting solution for finding the longest strictly increasing subsequence in an array.

leetcodedynamic-programmingbinary-searcharray
LeetCode 494: Target Sum

A clear explanation of counting sign assignments that reach a target using recursion first, then subset-sum dynamic programming.

leetcodearraydynamic-programmingbacktrackingknapsack
LeetCode 417: Pacific Atlantic Water Flow

A clear explanation of finding cells that can flow to both oceans using reverse graph traversal from the borders.

leetcodematrixdfsbfsgraph
LeetCode 467: Unique Substrings in Wraparound String

A clear explanation of counting unique substrings that appear in the infinite alphabet wraparound string using dynamic programming by ending character.

leetcodestringdynamic-programming
LeetCode 319: Bulb Switcher

A clear explanation of Bulb Switcher using divisor parity and perfect squares.

leetcodemathbrainteaser
LeetCode 299: Bulls and Cows

A counting solution for producing the Bulls and Cows hint while handling duplicate digits correctly.

leetcodehash-tablestringcounting
LeetCode 445: Add Two Numbers II

Add two numbers stored in forward-order linked lists using stacks and carry propagation.

leetcodelinked-liststackmath
LeetCode 348: Design Tic-Tac-Toe

A clear explanation of Design Tic-Tac-Toe using row, column, and diagonal counters for constant-time winner checks.

leetcodedesignarrayhash-tablematrix
LeetCode 394: Decode String

A clear explanation of decoding nested repeat expressions using a stack.

leetcodestringstackrecursionparser
LeetCode 369: Plus One Linked List

A clear explanation of adding one to a number stored as a linked list using the rightmost non-nine digit.

leetcodelinked-listmath
LeetCode 493: Reverse Pairs

A clear explanation of counting pairs where nums[i] is greater than twice nums[j] using merge sort.

leetcodearraydivide-and-conquermerge-sortbinary-indexed-tree
LeetCode 318: Maximum Product of Word Lengths

A clear explanation of Maximum Product of Word Lengths using bit masks to test disjoint character sets efficiently.

leetcodearraystringbit-manipulation
LeetCode 416: Partition Equal Subset Sum

A clear explanation of deciding whether an array can be split into two equal-sum subsets using 0/1 knapsack dynamic programming.

leetcodearraydynamic-programmingknapsack
LeetCode 393: UTF-8 Validation

A clear explanation of validating a byte sequence as UTF-8 using bit masks and a continuation-byte counter.

leetcodearraybit-manipulationutf-8
LeetCode 444: Sequence Reconstruction

Check whether nums is the unique shortest supersequence of given subsequences using topological sorting.

leetcodegraphtopological-sortbfsqueue
LeetCode 466: Count The Repetitions

A clear explanation of counting how many repeated copies of one string can be obtained as a subsequence of another repeated string.

leetcodestringdynamic-programmingsimulation
LeetCode 368: Largest Divisible Subset

A clear explanation of finding the largest subset where every pair is divisible using sorting, dynamic programming, and parent reconstruction.

leetcodearraymathdynamic-programmingsorting
LeetCode 492: Construct the Rectangle

A clear explanation of finding rectangle dimensions with a fixed area and the smallest length-width difference.

leetcodemathfactorization
LeetCode 347: Top K Frequent Elements

A clear explanation of Top K Frequent Elements using frequency counting and bucket sort.

leetcodearrayhash-tablebucket-sortheap
LeetCode 298: Binary Tree Longest Consecutive Sequence

A DFS solution for finding the longest parent-to-child path where each node value increases by exactly one.

leetcodetreebinary-treedfs
LeetCode 297: Serialize and Deserialize Binary Tree

A preorder DFS codec for converting a binary tree to a string and reconstructing the same tree from that string.

leetcodetreebinary-treedfsdesignstring
LeetCode 225: Implement Stack using Queues

A clear explanation of implementing a LIFO stack using only FIFO queue operations.

leetcodestackqueuedesign
LeetCode 443: String Compression

Compress a character array in-place using two pointers and grouped character counting.

leetcodearraystringtwo-pointers
LeetCode 392: Is Subsequence

A clear explanation of checking whether one string is a subsequence of another using two pointers.

leetcodestringtwo-pointersdynamic-programming
LeetCode 367: Valid Perfect Square

A clear explanation of checking whether an integer is a perfect square using binary search without sqrt.

leetcodemathbinary-search
LeetCode 415: Add Strings

A clear explanation of adding two non-negative integer strings using manual digit-by-digit simulation.

leetcodemathstringsimulationtwo-pointers
LeetCode 317: Shortest Distance from All Buildings

A clear explanation of Shortest Distance from All Buildings using BFS from each building with distance and reach accumulation.

leetcodematrixbfsgraph
LeetCode 491: Non-decreasing Subsequences

A clear explanation of generating all distinct non-decreasing subsequences using DFS, backtracking, and per-level duplicate control.

leetcodearraybacktrackinghash-table
LeetCode 346: Moving Average from Data Stream

A clear explanation of Moving Average from Data Stream using a queue and rolling sum.

leetcodequeuedesignsliding-window
LeetCode 465: Optimal Account Balancing

A clear explanation of minimizing debt-settlement transactions using net balances, backtracking, and memoization-style pruning.

leetcodearrayhash-mapbacktrackingdfs
LeetCode 224: Basic Calculator

A clear explanation of evaluating an expression with plus, minus, spaces, and parentheses using a stack.

leetcodestringstackparser
LeetCode 296: Best Meeting Point

A median-based solution for minimizing total Manhattan distance in a grid.

leetcodemathsortingmatrixmedian
LeetCode 223: Rectangle Area

A clear explanation of computing the total covered area of two axis-aligned rectangles by subtracting their overlap.

leetcodemathgeometry
LeetCode 442: Find All Duplicates in an Array

Find all duplicated numbers in an array in O(n) time and O(1) extra space using index marking.

leetcodearrayhashingin-place
LeetCode 441: Arranging Coins

Find the maximum number of complete staircase rows that can be formed using binary search and triangular numbers.

leetcodemathbinary-search
LeetCode 414: Third Maximum Number

A clear explanation of finding the third distinct maximum number using one pass and constant space.

leetcodearraysortingset
LeetCode 345: Reverse Vowels of a String

A clear explanation of Reverse Vowels of a String using two pointers and selective swaps.

leetcodestringtwo-pointers
LeetCode 366: Find Leaves of Binary Tree

A clear explanation of grouping binary tree nodes by the round in which they become leaves using postorder DFS.

leetcodetreedfspostorderbinary-tree
LeetCode 316: Remove Duplicate Letters

A clear explanation of Remove Duplicate Letters using a greedy monotonic stack.

leetcodestringstackgreedymonotonic-stack
LeetCode 391: Perfect Rectangle

A clear explanation of checking whether many small axis-aligned rectangles form one exact rectangular cover using area and corner parity.

leetcodearrayhash-tablegeometry
LeetCode 275: H-Index II

A clear explanation of the H-Index II problem using binary search on a sorted citations array.

leetcodearraybinary-search
LeetCode 295: Find Median from Data Stream

A two-heap data structure for adding numbers from a stream and returning the current median in constant time.

leetcodeheappriority-queuedesigndata-stream
LeetCode 222: Count Complete Tree Nodes

A clear explanation of counting nodes in a complete binary tree faster than visiting every node.

leetcodebinary-treerecursionbinary-search
LeetCode 490: The Maze

A clear explanation of deciding whether a rolling ball can stop at the destination using BFS or DFS over stopping cells.

leetcodegraphbfsdfsmatrixmaze
LeetCode 344: Reverse String

A clear explanation of Reverse String using two pointers and in-place swaps.

leetcodestringtwo-pointers
LeetCode 413: Arithmetic Slices

A clear explanation of counting arithmetic subarrays using dynamic programming and consecutive differences.

leetcodearraydynamic-programmingmath
LeetCode 464: Can I Win

A clear explanation of solving the Can I Win game using minimax recursion, bitmask state compression, and memoization.

leetcodedynamic-programmingmemoizationbitmaskgame-theory
LeetCode 315: Count of Smaller Numbers After Self

A clear explanation of Count of Smaller Numbers After Self using coordinate compression and a Fenwick Tree.

leetcodearrayfenwick-treebinary-indexed-treemerge-sort
LeetCode 440: K-th Smallest in Lexicographical Order

Find the k-th integer in lexicographical order without generating all numbers, using prefix counting over a conceptual trie.

leetcodetrieprefixmathlexicographical-order
LeetCode 365: Water and Jug Problem

A clear explanation of solving the Water and Jug Problem using Bézout's identity and greatest common divisor.

leetcodemathgcdbreadth-first-searchnumber-theory
LeetCode 390: Elimination Game

A clear explanation of finding the last remaining number after alternating left-to-right and right-to-left eliminations.

leetcodemathrecursionsimulation
LeetCode 274: H-Index

A clear explanation of the H-Index problem using sorting, then an optimized counting approach.

leetcodearraysortingcounting-sort
LeetCode 294: Flip Game II

A recursive game theory solution with memoization for deciding whether the starting player can force a win.

leetcodebacktrackinggame-theorymemoizationstring
LeetCode 221: Maximal Square

A clear explanation of finding the largest square of 1s in a binary matrix using dynamic programming.

leetcodedynamic-programmingmatrix
LeetCode 293: Flip Game

A simple string scanning solution for generating every possible next state after flipping one consecutive ++ pair into --.

leetcodestringsimulation
LeetCode 273: Integer to English Words

A clear explanation of the Integer to English Words problem using three-digit chunks and scale words.

leetcodemathstringrecursion
LeetCode 292: Nim Game

A game theory solution for deciding whether the first player can win by using the losing-position pattern of multiples of four.

leetcodemathgame-theory
LeetCode 220: Contains Duplicate III

A clear explanation of checking nearby indices with nearby values using a sliding window and bucket hashing.

leetcodearrayhash-mapbucket-sortsliding-window
LeetCode 272: Closest Binary Search Tree Value II

A clear explanation of the Closest Binary Search Tree Value II problem using inorder traversal and a fixed-size sliding window.

leetcodetreebinary-search-treeinorder-traversaldeque
LeetCode 291: Word Pattern II

A backtracking solution for matching a pattern string to a target string using a bijective character-to-substring mapping.

leetcodebacktrackinghash-tablestring
LeetCode 219: Contains Duplicate II

A clear explanation of detecting whether equal values appear within distance k using a hash map or sliding window set.

leetcodearrayhash-maphash-setsliding-window
LeetCode 290: Word Pattern

A hash map solution for checking whether a pattern string and a space-separated word string form a bijection.

leetcodehash-tablestring
LeetCode 271: Encode and Decode Strings

A clear explanation of the Encode and Decode Strings problem using length-prefix encoding.

leetcodestringdesignencodingdecoding
LeetCode 289: Game of Life

An in-place matrix simulation for computing the next state of Conway's Game of Life using temporary encoded states.

leetcodematrixsimulationin-place
LeetCode 242: Valid Anagram

A clear explanation of checking whether two strings are anagrams using character frequency counting.

leetcodestringhash-tablecounting
LeetCode 270: Closest Binary Search Tree Value

A clear explanation of the Closest Binary Search Tree Value problem using the BST property to walk toward the target.

leetcodetreebinary-search-treedfsbinary-search
LeetCode 218: The Skyline Problem

A clear explanation of computing the skyline formed by buildings using sweep line and a max-heap.

leetcodeheapsweep-linegeometrypriority-queue
LeetCode 269: Alien Dictionary

A clear explanation of the Alien Dictionary problem using graph construction and topological sorting.

leetcodegraphtopological-sortbfsstring
LeetCode 217: Contains Duplicate

A clear explanation of detecting duplicates in an array using a hash set and sorting.

leetcodearrayhash-setsorting
LeetCode 241: Different Ways to Add Parentheses

A clear explanation of generating all possible results from different parenthesizations using divide and conquer recursion.

leetcodedivide-and-conquerrecursionmemoizationexpression-parsing
LeetCode 268: Missing Number

A clear explanation of the Missing Number problem using sum formula and XOR.

leetcodearraymathbit-manipulationxor
LeetCode 216: Combination Sum III

A clear explanation of finding k distinct numbers from 1 to 9 that sum to n using backtracking.

leetcodearraybacktrackingdfs
LeetCode 288: Unique Word Abbreviation

A hash map design for checking whether a word's abbreviation is unique in a dictionary.

leetcodehash-tablestringdesign
LeetCode 240: Search a 2D Matrix II

A clear explanation of searching a row-sorted and column-sorted matrix using the top-right corner elimination method.

leetcodearraymatrixbinary-search
LeetCode 215: Kth Largest Element in an Array

A clear explanation of finding the kth largest element using sorting, a min-heap, and Quickselect.

leetcodearrayheapquickselectdivide-and-conquer
LeetCode 267: Palindrome Permutation II

A clear explanation of the Palindrome Permutation II problem using character counts and backtracking over half of the palindrome.

leetcodebacktrackingstringhash-tablepermutation