brain

tamnd's digital brain — notes, problems, research

42734 notes

LeetCode 1035: Uncrossed Lines

A clear explanation of maximizing uncrossed connecting lines between two arrays using longest common subsequence dynamic programming.

leetcodearraydynamic-programming
LeetCode 1012: Numbers With Repeated Digits

A clear explanation of counting numbers up to n with at least one repeated digit using digit DP and combinatorics.

leetcodemathdynamic-programmingcombinatorics
LeetCode 1022: Sum of Root To Leaf Binary Numbers

A clear explanation of summing root-to-leaf binary numbers in a binary tree using DFS with accumulated values.

leetcodetreedepth-first-searchbinary-tree
LeetCode 1018: Binary Prefix Divisible By 5

A clear explanation of checking divisibility of binary prefixes by 5 using running remainder tracking.

leetcodearraybit-manipulation
LeetCode 1032: Stream of Characters

A clear explanation of efficiently querying a stream of characters against a word list using an Aho-Corasick trie.

leetcodearraystringdesigntriestring-matching
LeetCode 1026: Maximum Difference Between Node and Ancestor

A clear explanation of finding the maximum ancestor-node difference in a binary tree by tracking min and max along each root-to-leaf path.

leetcodetreedepth-first-searchbinary-tree
LeetCode 1008: Construct Binary Search Tree from Preorder Traversal

A clear explanation of reconstructing a BST from its preorder traversal using value range bounds.

leetcodearraytreebinary-search-treebinary-treedivide-and-conquer
LeetCode 1048: Longest String Chain

A clear explanation of finding the longest word chain where each word is formed by inserting one letter into the previous word, using dynamic programming.

leetcodearrayhash-tabletwo-pointersstringdynamic-programming
LeetCode 1054: Distant Barcodes

A clear explanation of rearranging barcodes so no two adjacent barcodes are equal using a greedy max-heap approach.

leetcodearrayhash-tablegreedysortingheap
LeetCode 1043: Partition Array for Maximum Sum

A clear explanation of maximizing array sum by partitioning into subarrays of at most k elements, each filled with their maximum value, using dynamic programming.

leetcodearraydynamic-programming
LeetCode 1033: Moving Stones Until Consecutive

A clear explanation of finding the minimum and maximum moves to make three stones consecutive by analyzing gap cases.

leetcodemathbrainteasers
LeetCode 1013: Partition Array Into Three Parts With Equal Sum

A clear explanation of checking if an array can be split into three contiguous parts with equal sum using a greedy two-pass approach.

leetcodearraygreedy
LeetCode 1090: Largest Values From Labels

A clear explanation of selecting the maximum sum subset under item and label count constraints using a greedy approach.

leetcodearrayhash-tablegreedysortingcounting
LeetCode 1091: Shortest Path in Binary Matrix

A clear explanation of finding the shortest path from top-left to bottom-right in a binary matrix using BFS.

leetcodearraybreadth-first-searchmatrix
LeetCode 1061: Lexicographically Smallest Equivalent String

A clear explanation of finding the lexicographically smallest equivalent string using Union-Find with canonical representatives.

leetcodestringunion-find
LeetCode 1002: Find Common Characters

A clear explanation of finding characters that appear in all words using minimum frequency counts.

leetcodearrayhash-tablestring
LeetCode 1059: All Paths from Source Lead to Destination

A clear explanation of verifying that all paths from a source node lead to a destination using DFS with cycle detection.

leetcodedepth-first-searchgraph
LeetCode 1088: Confusing Number II

A clear explanation of counting confusing numbers up to n using digit backtracking with rotation validation.

leetcodemathbacktracking
LeetCode 1017: Convert to Base -2

A clear explanation of converting a non-negative integer to its base negative-two representation.

leetcodemath
LeetCode 1014: Best Sightseeing Pair

A clear explanation of maximizing the sightseeing score by tracking the best left value seen so far in a single pass.

leetcodearraydynamic-programming
LeetCode 1085: Sum of Digits in the Minimum Number

A clear explanation of checking if the digit sum of the array minimum is odd or even.

leetcodearraymath
LeetCode 1052: Grumpy Bookstore Owner

A clear explanation of maximizing satisfied customers by choosing the best window for the owner to not be grumpy using a sliding window.

leetcodearraysliding-window
LeetCode 1062: Longest Repeating Substring

A clear explanation of finding the longest substring that appears at least twice using binary search on length with rolling hash.

leetcodestringbinary-searchdynamic-programmingrolling-hashsuffix-array
LeetCode 1095: Find in Mountain Array

A clear explanation of finding a target in a mountain array using three binary searches on the interface API.

leetcodearraybinary-searchinteractive
LeetCode 1016: Binary String With Substrings Representing 1 To N

A clear explanation of checking whether a binary string contains all binary representations of integers from 1 to n.

leetcodestringbinary-search
LeetCode 1075: Project Employees I

A clear explanation of computing the average years of experience per project using JOIN and AVG aggregation.

leetcodedatabase
LeetCode 1092: Shortest Common Supersequence

A clear explanation of finding the shortest string containing both input strings as subsequences using LCS dynamic programming.

leetcodestringdynamic-programming
LeetCode 1019: Next Greater Node In Linked List

A clear explanation of finding the next greater value for each node in a linked list using a monotonic stack.

leetcodearraylinked-liststackmonotonic-stack
LeetCode 1079: Letter Tile Possibilities

A clear explanation of counting all distinct non-empty sequences from a set of letter tiles using backtracking with frequency counting.

leetcodehash-tablestringbacktrackingcounting
LeetCode 1098: Unpopular Books

A clear explanation of finding books with fewer than 10 sales in the last year that were not sold in the last year using LEFT JOIN and GROUP BY.

leetcodedatabase
LeetCode 1072: Flip Columns For Maximum Number of Equal Rows

A clear explanation of finding the maximum number of rows that can be made all-equal by flipping columns, using row pattern normalization.

leetcodearrayhash-tablematrix
LeetCode 1055: Shortest Way to Form String

A clear explanation of finding the minimum number of subsequences of source needed to form target using greedy two-pointer scanning.

leetcodestringgreedytwo-pointers
LeetCode 1023: Camelcase Matching

A clear explanation of checking camelCase pattern matching by verifying uppercase consistency with a two-pointer approach.

leetcodearraytriestringtwo-pointers
LeetCode 1045: Customers Who Bought All Products

A clear explanation of finding customers who purchased every product in the catalog using GROUP BY and HAVING with COUNT DISTINCT.

leetcodedatabase
LeetCode 1066: Campus Bikes II

A clear explanation of finding the minimum total Manhattan distance to assign bikes to workers using bitmask dynamic programming.

leetcodearraydynamic-programmingbit-manipulationbitmask
LeetCode 1093: Statistics from a Large Sample

A clear explanation of computing statistical measures (minimum, maximum, mean, median, mode) from a frequency count array.

leetcodearraymathprobability-and-statistics
LeetCode 1076: Project Employees II

A clear explanation of finding the project with the most employees using GROUP BY, COUNT, and a subquery for the maximum.

leetcodedatabase
LeetCode 1038: Binary Search Tree to Greater Sum Tree

A clear explanation of converting a BST to a greater sum tree by accumulating values in reverse inorder traversal.

leetcodetreedepth-first-searchbinary-search-treebinary-tree
LeetCode 1086: High Five

A clear explanation of computing each student's top-5 average score using sorting and grouping.

leetcodearrayhash-tablesorting
LeetCode 1056: Confusing Number

A clear explanation of checking if a number becomes a different valid number when rotated 180 degrees.

leetcodemath
LeetCode 1051: Height Checker

A clear explanation of counting students not in the expected height order by comparing the array to its sorted version.

leetcodearraysortingcounting-sort
LeetCode 1030: Matrix Cells in Distance Order

A clear explanation of sorting matrix cells by Chebyshev distance from a given center cell using BFS.

leetcodearraymathsortingmatrix
LeetCode 1084: Sales Analysis III

A clear explanation of finding products sold only in the first quarter of 2019 using GROUP BY with date range conditions.

leetcodedatabase
LeetCode 1057: Campus Bikes

A clear explanation of greedily assigning bikes to workers based on Manhattan distance, prioritizing by distance then worker then bike index.

leetcodearraygreedysorting
LeetCode 1073: Adding Negative Numbers

A clear explanation of adding two non-positive integers represented as arrays of digits.

leetcodearraymathsimulation
LeetCode 1064: Fixed Point

A clear explanation of finding the smallest index where arr[i] equals i using binary search on a sorted distinct array.

leetcodearraybinary-search
LeetCode 1065: Index Pairs of a String

A clear explanation of finding all index pairs where a word from the list appears in a text string using a trie.

leetcodestringtriesorting
LeetCode 1046: Last Stone Weight

A clear explanation of simulating stone smashing to find the last remaining weight using a max heap.

leetcodearrayheappriority-queue
LeetCode 1034: Coloring A Border

A clear explanation of coloring the border of a connected component in a grid using BFS.

leetcodearraydepth-first-searchbreadth-first-searchmatrix
LeetCode 1041: Robot Bounded In Circle

A clear explanation of determining if a robot stays in a bounded circle by checking position and direction after one instruction cycle.

leetcodemathstringsimulation
LeetCode 1047: Remove All Adjacent Duplicates In String

A clear explanation of eliminating adjacent duplicate character pairs from a string using a stack.

leetcodestringstack
LeetCode 1001: Grid Illumination

A clear explanation of simulating lamp illumination on a grid using hash maps for rows, columns, and diagonals.

leetcodearrayhash-tablesimulation
LeetCode 1021: Remove Outermost Parentheses

A clear explanation of removing outermost parentheses from each primitive decomposition by tracking nesting depth.

leetcodestringstack
LeetCode 1004: Max Consecutive Ones III

A clear explanation of finding the longest subarray of ones by flipping at most k zeros using a sliding window.

leetcodearraybinary-searchsliding-windowprefix-sum
LeetCode 1089: Duplicate Zeros

A clear explanation of duplicating zeros in-place in an array without using extra space by working backwards.

leetcodearraytwo-pointers
LeetCode 1010: Pairs of Songs With Total Durations Divisible by 60

A clear explanation of counting song pairs whose total duration is divisible by 60 using remainder frequency counting.

leetcodearrayhash-tablecounting
LeetCode 1028: Recover a Tree From Preorder Traversal

A clear explanation of reconstructing a binary tree from a depth-encoded preorder traversal string using a stack.

leetcodestringtreedepth-first-searchbinary-tree
LeetCode 1097: Game Play Analysis V

A clear explanation of finding the fraction of players retained the day after their first login using self-join and window functions.

leetcodedatabase
LeetCode 1068: Product Sales Analysis I

A clear explanation of retrieving product names and their sale years using a JOIN between Sales and Product tables.

leetcodedatabase
LeetCode 1015: Smallest Integer Divisible by K

A clear explanation of finding the smallest repunit divisible by K by tracking remainders to detect cycles.

leetcodehash-tablemath
LeetCode 1099: Two Sum Less Than K

A clear explanation of finding the maximum sum of two numbers less than k using a two-pointer approach on a sorted array.

leetcodearraytwo-pointerssortingbinary-search
LeetCode 1037: Valid Boomerang

A clear explanation of checking if three points form a boomerang (non-collinear) using the cross product.

leetcodearraymathgeometry
LeetCode 1074: Number of Submatrices That Sum to Target

A clear explanation of counting submatrices with a given sum using 2D prefix sums combined with the subarray sum equals k technique.

leetcodearrayhash-tabledynamic-programmingmatrixprefix-sum
LeetCode 1009: Complement of Base 10 Integer

A clear explanation of finding the complement of a number by XORing with a bitmask of the same bit length.

leetcodebit-manipulation
LeetCode 1011: Capacity To Ship Packages Within D Days

A clear explanation of finding the minimum ship capacity to deliver all packages within D days using binary search.

leetcodearraybinary-search
LeetCode 1040: Moving Stones Until Consecutive II

A clear explanation of finding minimum and maximum moves to make stones consecutive using a sliding window.

leetcodearraymathtwo-pointerssortingsliding-window
LeetCode 1024: Video Stitching

A clear explanation of finding the minimum number of video clips to cover a time range using a greedy interval covering approach.

leetcodearraydynamic-programminggreedy
LeetCode 1087: Brace Expansion

A clear explanation of generating all strings from a brace expansion pattern in lexicographic order using backtracking.

leetcodestringbacktrackingbreadth-first-search
LeetCode 1050: Actors and Directors Who Cooperated At Least Three Times

A clear explanation of finding actor-director pairs with at least three collaborations using GROUP BY and HAVING.

leetcodedatabase
LeetCode 1049: Last Stone Weight II

A clear explanation of minimizing the last stone weight by splitting stones into two groups using 0/1 knapsack dynamic programming.

leetcodearraydynamic-programming
LeetCode 1063: Number of Valid Subarrays

A clear explanation of counting subarrays where the leftmost element is not larger than any other element, using a monotonic stack.

leetcodearraystackmonotonic-stack
LeetCode 1083: Sales Analysis II

A clear explanation of finding buyers who bought an iPhone but not an iPad using JOIN and NOT IN filtering.

leetcodedatabase
LeetCode 1070: Product Sales Analysis III

A clear explanation of finding the first year each product was sold using a self-join or window function.

leetcodedatabase
LeetCode 1042: Flower Planting With No Adjacent

A clear explanation of assigning 4 flower types to garden nodes with no adjacent conflicts using greedy graph coloring.

leetcodedepth-first-searchbreadth-first-searchgraph
LeetCode 1053: Previous Permutation With One Swap

A clear explanation of finding the lexicographically largest permutation smaller than the given array using at most one swap.

leetcodearraygreedy
LeetCode 1007: Minimum Domino Rotations For Equal Row

A clear explanation of finding minimum rotations to make all tops or bottoms equal using a greedy candidate check.

leetcodearraygreedy
LeetCode 1081: Smallest Subsequence of Distinct Characters

A clear explanation of finding the lexicographically smallest subsequence with all distinct characters using a greedy stack approach.

leetcodestringstackgreedymonotonic-stack
LeetCode 1060: Missing Element in Sorted Array

A clear explanation of finding the k-th missing number in a sorted array using binary search on the missing count.

leetcodearraybinary-search
LeetCode 1096: Brace Expansion II

A clear explanation of generating all strings from a brace expansion expression using recursive parsing and set union/concatenation.

leetcodestringbacktrackingstack
LeetCode 1082: Sales Analysis I

A clear explanation of finding the best seller(s) by total price using GROUP BY, SUM, and a subquery for the maximum.

leetcodedatabase
LeetCode 1027: Longest Arithmetic Subsequence

A clear explanation of finding the longest arithmetic subsequence in an array using dynamic programming with difference hash maps.

leetcodearrayhash-tablebinary-searchdynamic-programming
LeetCode 1005: Maximize Sum Of Array After K Negations

A clear explanation of maximizing array sum after exactly k negations using a greedy strategy.

leetcodearraygreedysorting
LeetCode 1025: Divisor Game

A clear explanation of why Alice wins the divisor game if and only if n is even, proven by mathematical induction.

leetcodemathdynamic-programmingbrainteasers
Appendix E. Memory Safety Checklist

Zig gives you direct control over memory. That control is useful, but it also means you must follow clear rules.

zigbook
Semantic Analysis

Semantic analysis is the compiler stage that checks what a program means.

zigbook
Debug Builds

A debug build is a build made for finding mistakes.

zigbook
WebAssembly

WebAssembly, often shortened to Wasm, is a portable binary instruction format. It lets you compile code once and run it inside different hosts, such as web browsers, servers,...

zigbook
Memory Safety in Zig

Memory safety means using memory only while it is valid, only through the right type, and only inside the allowed range.

zigbook
Allocation Failure Handling

Allocation can fail.

zigbook
`@bitCast`

@bitCast reinterprets the bits of one value as another type.

zigbook
Static Dispatch

Static dispatch means the compiler decides which code to call before the program runs.

zigbook
Type Reflection Basics

Type reflection means asking questions about a type while Zig is compiling the program.

zigbook
Data-Oriented Design

Data-oriented design means you organize a program around the data it processes.

zigbook
Efficient Text Processing

Efficient text processing means working with text without doing unnecessary allocation, copying, or decoding.

zigbook
Why SIMD Matters

SIMD means Single Instruction, Multiple Data.

zigbook
Embedded Development

Embedded development means writing software for small computers inside devices.

zigbook
Appendix D. Important Standard Library APIs

Zig’s standard library is imported with:

zigbook
Custom Allocators

A custom allocator is an allocator you design for a specific memory policy.

zigbook
`@ptrCast`

@ptrCast converts one pointer type into another pointer type.

zigbook
Dangling Pointers

A dangling pointer is a pointer that refers to memory that is no longer valid.

zigbook