brain

tamnd's digital brain — notes, problems, research

43815 notes

LeetCode 239: Sliding Window Maximum

A clear explanation of finding the maximum value in every sliding window using a monotonic deque.

leetcodearraysliding-windowdequemonotonic-queue
LeetCode 214: Shortest Palindrome

A clear explanation of building the shortest palindrome by finding the longest palindromic prefix using KMP.

leetcodestringkmppalindrome
LeetCode 287: Find the Duplicate Number

A Floyd cycle detection solution for finding the repeated number without modifying the array and using constant extra space.

leetcodearraytwo-pointerscycle-detectionfloyd
LeetCode 266: Palindrome Permutation

A clear explanation of the Palindrome Permutation problem using character parity counting.

leetcodehash-tablestringcounting
LeetCode 286: Walls and Gates

A multi-source BFS solution for filling each empty room with its shortest distance to the nearest gate.

leetcodebreadth-first-searchgraphmatrixqueue
LeetCode 238: Product of Array Except Self

A clear explanation of computing each product except self using prefix and suffix products without division.

leetcodearrayprefix-productsuffix-product
LeetCode 213: House Robber II

A clear explanation of maximizing robbed money from circularly arranged houses using dynamic programming.

leetcodedynamic-programmingarray
LeetCode 100: Same Tree

A detailed guide to solving Same Tree with recursive DFS and structural comparison.

leetcodetreebinary-treedepth-first-searchbreadth-first-search
LeetCode 99: Recover Binary Search Tree

A detailed guide to solving Recover Binary Search Tree with inorder traversal and two misplaced nodes.

leetcodetreebinary-search-treedepth-first-search
LeetCode 125: Valid Palindrome

A clear explanation of checking whether a string is a palindrome after ignoring non-alphanumeric characters and case.

leetcodestringtwo-pointers
LeetCode 98: Validate Binary Search Tree

A detailed guide to solving Validate Binary Search Tree with recursive lower and upper bounds.

leetcodetreebinary-search-treedepth-first-searchrecursion
LeetCode 150: Evaluate Reverse Polish Notation

Evaluate an arithmetic expression written in Reverse Polish Notation using a stack.

leetcodemediumstackmath
LeetCode 149: Max Points on a Line

Find the maximum number of points lying on the same straight line using slope counting and normalization.

leetcodehardgeometryhash-mapmath
LeetCode 124: Binary Tree Maximum Path Sum

A clear explanation of finding the maximum path sum in a binary tree using bottom-up depth-first search.

leetcodetreebinary-treedfsdynamic-programming
LeetCode 123: Best Time to Buy and Sell Stock III

A clear explanation of maximizing stock profit with at most two transactions using dynamic programming.

leetcodearraydynamic-programming
LeetCode 97: Interleaving String

A detailed guide to solving Interleaving String with two-dimensional dynamic programming.

leetcodestringdynamic-programming
LeetCode 122: Best Time to Buy and Sell Stock II

A clear explanation of maximizing stock profit with unlimited transactions using a greedy single-pass method.

leetcodearraygreedydynamic-programming
LeetCode 96: Unique Binary Search Trees

A detailed guide to solving Unique Binary Search Trees with dynamic programming and the Catalan recurrence.

leetcodetreebinary-search-treedynamic-programmingmath
LeetCode 75: Sort Colors

A clear guide to sorting an array of 0s, 1s, and 2s in place using the Dutch National Flag algorithm.

leetcodearraytwo-pointerssorting
LeetCode 148: Sort List

Sort a singly linked list in ascending order using merge sort with fast and slow pointers.

leetcodemediumlinked-listtwo-pointersdivide-and-conquermerge-sort
LeetCode 121: Best Time to Buy and Sell Stock

A clear explanation of finding the maximum profit from one stock transaction using a single pass.

leetcodearraydynamic-programming
LeetCode 174: Dungeon Game

A clear explanation of computing the minimum initial health needed to survive a dungeon using reverse dynamic programming.

leetcodedynamic-programmingmatrix
LeetCode 74: Search a 2D Matrix

A clear guide to searching a sorted 2D matrix using binary search over a virtual one-dimensional array.

leetcodearraymatrixbinary-search
LeetCode 147: Insertion Sort List

Sort a singly linked list using insertion sort by splicing each node into a growing sorted list.

leetcodemediumlinked-listsortinginsertion-sort
LeetCode 120: Triangle

A clear explanation of finding the minimum path sum in a triangle using bottom-up dynamic programming.

leetcodedynamic-programmingarray
LeetCode 173: Binary Search Tree Iterator

A clear explanation of designing an iterator over a BST using controlled inorder traversal with a stack.

leetcodestacktreedesignbinary-search-treeiterator
LeetCode 95: Unique Binary Search Trees II

A detailed guide to solving Unique Binary Search Trees II with recursive tree generation over value ranges.

leetcodetreebinary-search-treedynamic-programmingbacktrackingrecursion
LeetCode 146: LRU Cache

Design an LRU cache with O(1) get and put operations using a hash map and doubly linked list.

leetcodemediumdesignhash-maplinked-list
LeetCode 172: Factorial Trailing Zeroes

A clear explanation of counting trailing zeroes in n! by counting factors of 5 instead of computing the factorial directly.

leetcodemath
LeetCode 73: Set Matrix Zeroes

A clear guide to setting matrix rows and columns to zero in place using the first row and first column as markers.

leetcodearraymatrixin-place
LeetCode 94: Binary Tree Inorder Traversal

A detailed guide to solving Binary Tree Inorder Traversal with recursion and an iterative stack.

leetcodebinary-treedepth-first-searchstackrecursion
LeetCode 145: Binary Tree Postorder Traversal

Return the postorder traversal of a binary tree using recursion or an iterative stack-based approach.

leetcodeeasytreebinary-treedfsstack
LeetCode 171: Excel Sheet Column Number

A clear explanation of converting an Excel column title into its numeric index using base 26 accumulation.

leetcodemathstringbase-conversion
LeetCode 72: Edit Distance

A clear guide to computing the minimum number of insert, delete, and replace operations needed to convert one string into another.

leetcodedynamic-programmingstring
LeetCode 144: Binary Tree Preorder Traversal

Return the preorder traversal of a binary tree using recursion or an explicit stack.

leetcodeeasytreebinary-treedfsstack
LeetCode 170: Two Sum III - Data Structure Design

A clear explanation of designing a data structure that supports add and find operations for pair sums.

leetcodehash-tabledesign
LeetCode 93: Restore IP Addresses

A detailed guide to solving Restore IP Addresses with backtracking over four valid IP segments.

leetcodestringbacktracking
LeetCode 71: Simplify Path

A clear guide to simplifying Unix-style file paths using a stack.

leetcodestringstack
LeetCode 119: Pascal's Triangle II

A clear explanation of generating a single row of Pascal's Triangle using in-place dynamic programming.

leetcodearraydynamic-programmingmath
LeetCode 200: Number of Islands

A clear explanation of counting connected groups of land cells in a grid using DFS or BFS.

leetcodematrixgraphdepth-first-searchbreadth-first-search
LeetCode 169: Majority Element

A clear explanation of finding the element that appears more than half the time using Boyer-Moore voting.

leetcodearrayhash-tabledivide-and-conquersortingcounting
LeetCode 143: Reorder List

Reorder a singly linked list in-place by finding the middle, reversing the second half, and merging the two halves alternately.

leetcodemediumlinked-listtwo-pointers
LeetCode 118: Pascal's Triangle

A clear explanation of generating Pascal's Triangle row by row using dynamic programming.

leetcodearraydynamic-programmingmath
LeetCode 92: Reverse Linked List II

A detailed guide to solving Reverse Linked List II with a dummy node and in-place sublist reversal.

leetcodelinked-list
LeetCode 70: Climbing Stairs

A clear guide to counting distinct ways to climb stairs using dynamic programming.

leetcodedynamic-programmingmathfibonacci
LeetCode 199: Binary Tree Right Side View

A clear explanation of returning the visible nodes from the right side of a binary tree using level-order traversal.

leetcodebinary-treebreadth-first-searchdepth-first-search
LeetCode 198: House Robber

A clear explanation of maximizing robbery profit without robbing adjacent houses using dynamic programming.

leetcodedynamic-programmingarray
LeetCode 142: Linked List Cycle II

Find the node where a linked list cycle begins using Floyd’s tortoise and hare algorithm with cycle entry mathematics.

leetcodemediumlinked-listtwo-pointersmath
LeetCode 117: Populating Next Right Pointers in Each Node II

A clear explanation of connecting next pointers in any binary tree using constant extra space.

leetcodetreebinary-treelinked-listbfs
LeetCode 91: Decode Ways

A detailed guide to solving Decode Ways with dynamic programming and careful handling of zeroes.

leetcodestringdynamic-programming
LeetCode 69: Sqrt(x)

A clear guide to computing the integer square root using binary search without built-in exponent functions.

leetcodemathbinary-search
LeetCode 168: Excel Sheet Column Title

A clear explanation of converting a positive integer into an Excel column title using bijective base 26.

leetcodemathstringbase-conversion
LeetCode 116: Populating Next Right Pointers in Each Node

A clear explanation of connecting next pointers in a perfect binary tree using constant extra space.

leetcodetreebinary-treebfslinked-list
LeetCode 167: Two Sum II - Input Array Is Sorted

A clear explanation of finding two numbers in a sorted array using two pointers and constant extra space.

leetcodearraytwo-pointersbinary-search
LeetCode 115: Distinct Subsequences

A clear explanation of counting distinct subsequences using dynamic programming.

leetcodedynamic-programmingstring
LeetCode 141: Linked List Cycle

Detect whether a linked list contains a cycle using Floyd’s tortoise and hare two-pointer algorithm.

leetcodeeasylinked-listtwo-pointers
LeetCode 114: Flatten Binary Tree to Linked List

A clear explanation of flattening a binary tree into a linked list in preorder traversal order using recursive depth-first search.

leetcodetreebinary-treelinked-listdfsrecursion
LeetCode 90: Subsets II

A detailed guide to solving Subsets II with sorting, backtracking, and duplicate skipping.

leetcodearraybacktrackingbit-manipulation
LeetCode 68: Text Justification

A clear guide to formatting text with greedy line packing and even space distribution.

leetcodearraystringsimulationgreedy
LeetCode 191: Number of 1 Bits

A clear explanation of counting set bits in an integer using bit manipulation and Brian Kernighan's algorithm.

leetcodebit-manipulationinteger
LeetCode 166: Fraction to Recurring Decimal

A clear explanation of converting a fraction into decimal form and detecting repeating fractional parts with a hash map.

leetcodehash-tablemathstring
LeetCode 113: Path Sum II

A clear explanation of finding all root-to-leaf paths whose values add up to a target sum using depth-first search and backtracking.

leetcodetreebinary-treedfsbacktrackingrecursion
LeetCode 67: Add Binary

A clear guide to adding two binary strings using two pointers and a carry.

leetcodestringmathsimulationbinary
LeetCode 89: Gray Code

A detailed guide to solving Gray Code using the binary-to-Gray-code formula.

leetcodemathbit-manipulationbacktracking
LeetCode 112: Path Sum

A clear explanation of checking whether a binary tree has a root-to-leaf path whose values add up to a target sum.

leetcodetreebinary-treedfsrecursion
LeetCode 140: Word Break II

Return all valid sentences formed by inserting spaces into a string so every word belongs to the dictionary, using DFS with memoization.

leetcodehardstringdynamic-programmingbacktrackingmemoizationhash-set
LeetCode 66: Plus One

A clear guide to adding one to a large integer represented as an array of digits.

leetcodearraymath
LeetCode 139: Word Break

Decide whether a string can be segmented into dictionary words using dynamic programming over prefixes.

leetcodemediumstringdynamic-programminghash-settrie
LeetCode 111: Minimum Depth of Binary Tree

A clear explanation of finding the minimum depth of a binary tree using breadth-first search.

leetcodetreebinary-treebfsqueue
LeetCode 88: Merge Sorted Array

A detailed guide to solving Merge Sorted Array in-place by merging from the back with three pointers.

leetcodearraytwo-pointerssorting
LeetCode 65: Valid Number

A clear guide to validating whether a string is a valid number using grammar rules and one left-to-right scan.

leetcodestringfinite-state-machineparsing
LeetCode 265: Paint House II

A clear explanation of the Paint House II problem using optimized dynamic programming with minimum and second minimum tracking.

leetcodedynamic-programmingarrayoptimization
LeetCode 110: Balanced Binary Tree

A clear explanation of checking whether a binary tree is height-balanced using bottom-up depth-first search.

leetcodetreebinary-treedfsrecursion
LeetCode 138: Copy List with Random Pointer

Create a deep copy of a linked list with next and random pointers using hash maps or interleaved node cloning.

leetcodemediumlinked-listhash-map
LeetCode 165: Compare Version Numbers

A clear explanation of comparing version strings revision by revision while ignoring leading zeros.

leetcodestringtwo-pointers
LeetCode 40: Combination Sum II

A clear explanation of finding unique combinations that sum to a target when each array element may be used at most once.

leetcodearraybacktrackingsorting
LeetCode 212: Word Search II

A clear explanation of finding multiple words in a character board using a Trie and DFS backtracking.

leetcodetriedfsbacktrackingmatrix
LeetCode 412: Fizz Buzz

A clear explanation of the Fizz Buzz problem using direct simulation and divisibility checks.

leetcodemathstringsimulation
LeetCode 190: Reverse Bits

A clear explanation of reversing the bits of a 32-bit integer using bit manipulation.

leetcodebit-manipulationinteger
LeetCode 489: Robot Room Cleaner

A clear explanation of cleaning an unknown grid using DFS, relative coordinates, and physical backtracking.

leetcodedfsbacktrackingsimulationrobot
LeetCode 314: Binary Tree Vertical Order Traversal

A clear explanation of Binary Tree Vertical Order Traversal using BFS with column indices.

leetcodebinary-treebfshash-tabletree
LeetCode 463: Island Perimeter

A clear explanation of counting the perimeter of an island in a grid by adding land-cell edges and subtracting shared edges.

leetcodearraymatrixsimulationcounting
LeetCode 87: Scramble String

A detailed guide to solving Scramble String with recursive dynamic programming and memoization.

leetcodestringdynamic-programmingrecursionmemoization
LeetCode 343: Integer Break

A clear explanation of Integer Break using dynamic programming, with a note on the greedy math solution.

leetcodemathdynamic-programming
LeetCode 364: Nested List Weight Sum II

A clear explanation of computing inverse depth weighted sum using level-order traversal.

leetcodedfsbfsnested-listtree
LeetCode 389: Find the Difference

A clear explanation of finding the extra character added to a shuffled string using counting and XOR.

leetcodestringhash-tablebit-manipulation
LeetCode 64: Minimum Path Sum

A clear guide to finding the minimum path sum in a grid using dynamic programming.

leetcodedynamic-programmingmatrix
LeetCode 285: Inorder Successor in BST

A binary-search-style solution for finding the smallest node greater than p in a binary search tree.

leetcodebinary-search-treetreebinary-search
LeetCode 237: Delete Node in a Linked List

A clear explanation of deleting a node from a singly linked list when only that node is given.

leetcodelinked-listin-place
LeetCode 109: Convert Sorted List to Binary Search Tree

A clear explanation of converting a sorted linked list into a height-balanced binary search tree using slow and fast pointers.

leetcodelinked-listtreebinary-search-treedfsdivide-and-conquertwo-pointers
LeetCode 439: Ternary Expression Parser

Evaluate a nested ternary expression using a right-to-left stack parser.

leetcodestringstackparsing
LeetCode 264: Ugly Number II

A clear explanation of the Ugly Number II problem using dynamic programming with three pointers.

leetcodedynamic-programmingmaththree-pointers
LeetCode 137: Single Number II

Find the number that appears once when every other number appears three times using bit counting or finite-state bit manipulation.

leetcodemediumarraybit-manipulation
LeetCode 39: Combination Sum

A clear explanation of finding all unique combinations that sum to a target using backtracking.

leetcodearraybacktracking
LeetCode 211: Design Add and Search Words Data Structure

A clear explanation of designing a word dictionary with addWord and wildcard search using a Trie and DFS.

leetcodetriedfsbacktrackingdesign
LeetCode 411: Minimum Unique Word Abbreviation

A clear explanation of finding the shortest abbreviation that does not conflict with any dictionary word using bit masks.

leetcodebit-manipulationbacktrackingstringenumeration
LeetCode 189: Rotate Array

A clear explanation of rotating an array to the right by k steps using in-place reversal.

leetcodearraytwo-pointersin-place
LeetCode 164: Maximum Gap

A clear explanation of finding the maximum adjacent gap in sorted order using buckets and the pigeonhole principle.

leetcodearraybucket-sortradix-sortsorting
LeetCode 313: Super Ugly Number

A clear explanation of Super Ugly Number using dynamic programming with one pointer per prime.

leetcodedynamic-programmingmathheap
LeetCode 488: Zuma Game

A clear explanation of solving Zuma Game with DFS, memoization, and chain-removal simulation.

leetcodestringdfsmemoizationbacktracking