brain

tamnd's digital brain — notes, problems, research

43815 notes

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 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 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 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 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 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 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 1021: Remove Outermost Parentheses

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

leetcodestringstack
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 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 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 1100: Find K-Length Substrings With No Repeated Characters

A clear explanation of counting substrings of length k with all unique characters using a sliding window.

leetcodehash-tablestringsliding-window
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 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 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 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 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 1094: Car Pooling

A clear explanation of determining if a car can transport all passengers using a difference array for passenger count tracking.

leetcodearraysortingheapsimulationprefix-sum
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 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 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 1086: High Five

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

leetcodearrayhash-tablesorting
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 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 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 1088: Confusing Number II

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

leetcodemathbacktracking
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 1037: Valid Boomerang

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

leetcodearraymathgeometry
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 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 1039: Minimum Score Triangulation of Polygon

A clear explanation of minimizing the total score of triangulating a polygon using interval dynamic programming.

leetcodearraydynamic-programming
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 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 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 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 1077: Project Employees III

A clear explanation of finding the most experienced employee(s) for each project using a window function or correlated subquery.

leetcodedatabase
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 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 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 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 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 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 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 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 1075: Project Employees I

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

leetcodedatabase
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
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 1001: Grid Illumination

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

leetcodearrayhash-tablesimulation
LeetCode 1044: Longest Duplicate Substring

A clear explanation of finding the longest duplicate substring using binary search on length combined with Rabin-Karp rolling hash.

leetcodestringbinary-searchsliding-windowrolling-hashsuffix-array
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 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 1031: Maximum Sum of Two Non-Overlapping Subarrays

A clear explanation of finding two non-overlapping subarrays with maximum combined sum using prefix sums and running maximums.

leetcodearraydynamic-programmingsliding-windowprefix-sum
LeetCode 1006: Clumsy Factorial

A clear explanation of computing the clumsy factorial by simulating cyclic operators with a stack.

leetcodemathstacksimulation
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 1067: Digit Count in Range

A clear explanation of counting the occurrences of a specific digit in all numbers from 1 to n using digit dynamic programming.

leetcodemathdynamic-programming
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 1035: Uncrossed Lines

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

leetcodearraydynamic-programming
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 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 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 1023: Camelcase Matching

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

leetcodearraytriestringtwo-pointers
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 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
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
Building Generic Data Structures

A generic data structure is a data structure that works with more than one element type.

zigbook
Type Coercion

Type coercion means Zig converts a value from one type to another when the conversion is safe and well-defined.

zigbook
Modeling State Machines

A state machine is a simple way to describe a program that moves between fixed states.

zigbook
Building Dynamic Strings

A dynamic string is text whose length is not fixed ahead of time.

zigbook
Build Options

A build option is a value passed from the command line into build.zig.

zigbook
The Basic Difference

Queues and stacks are two simple ways to organize a collection of items.

zigbook
Page Allocator

The page allocator asks the operating system for memory directly.

zigbook
`@typeInfo`

@typeInfo asks the compiler for structured information about a type.

zigbook
Reflection with `@typeInfo`

Reflection means inspecting a type as data.

zigbook
Error Union Internals

An error union is a value that can contain either:

zigbook
Union Safety

A union stores one active field at a time.

zigbook
UTF-8 Processing

Zig string data is usually stored as UTF-8 bytes.

zigbook
Stack vs Heap

Zig programs use memory in different places. The two most important places are the stack and the heap.

zigbook
Terminal Programming

Terminal programming means writing programs that interact with the command line as more than simple text output.

zigbook
Build a Static File Server

A static file server is a program that reads files from a directory and sends them to a browser over HTTP.

zigbook
`@Type`

@Type builds a type from compile-time type information.

zigbook
Generating Code at Compile Time

Generating code at compile time means using Zig code to create specialized program behavior before the final executable is built.

zigbook
`anytype`

anytype means the function parameter can accept many different types.

zigbook
Tagged Unions

A tagged union is a type that can store one value from several possible shapes.

zigbook