🐐
LeetGoat
Interview Prep
Competitive Programming
Discord
Golden References
Range queries
2D range queries
Sets
Union find
Strings
Tree queries
Tree helpers
Graphs
Matrices
Math
Primes
Bit ops & Bitset
Geometry
Misc.
Golden References
Persistent Segment Tree
Persistent segment tree (pointers)
Basic implementation of persistent segment tree over indices with pointers, see CSES range queries and copies for more info
Mo's on Tree
Mo's on tree (Codeforces 852I, Dating)
Reasonable worked example of order/tin/tout for path queries as subarray queries, also shows mo's add/remove/toggle and how we manually add back in the lca for some of the queries
Euler Tour Path Queries
Euler tour point add + path sum (Library Checker Vertex Add Path Sum)
Worked example of path sums between any two nodes with the entry/exit Euler tour: path sum = rootSum(a) + rootSum(b) - rootSum(lca) - rootSum(parent of lca), where rootSum(node) is the range sum up to tin[node]; a node add is two point writes
Euler Paths & Circuits
Euler circuit on a directed graph (CSES De Bruijn Sequence)
Worked example of Hierholzer: nodes are the (n-1)-bit strings and each n-bit string is an edge from its prefix to its suffix; dfs takes any unused edge and pushes a node to the front of the path only once all its edges are used
Euler circuit on an undirected graph (CSES Mail Delivery)
Worked example of Hierholzer on an undirected graph: check even degrees, then dfs with edge ids and a used array so each edge's second copy is skipped, then check the path holds all m + 1 nodes
Euler path on a directed graph (CSES Teleporters Path)
Worked example of Hierholzer for a directed Euler path: check start has out = in + 1, end has in = out + 1, all others balanced, then dfs from the start and check the path holds all m + 1 nodes
Floyd-Warshall
Floyd-Warshall (LeetCode 1334, Find the City With the Smallest Number of Neighbors at a Threshold Distance)
Worked example of all-pairs shortest paths as a recurrence over which cities may be middle stops: the best route using stops 0..k either avoids k, or goes through k once and splits into two routes that only use stops 0..k-1
0-1 BFS
0-1 BFS (LeetCode 2290, Minimum Obstacle Removal to Reach Corner)
Worked example of 0-1 BFS: Dijkstra with a deque instead of a heap, 0-cost moves pushed to the front and 1-cost moves to the back; relax on push, skip stale entries on pop
Topological Sort
Topological sort, Kahn's algorithm (LeetCode 207, Course Schedule)
Worked example of Kahn's algorithm: count each course's unfinished prerequisites, start from the courses with none, finishing a course lowers the count of every course that needs it; fewer than n finished means a cycle
Functional Graphs
Functional graph cycles + tails (CSES Planets Queries II)
Worked example of splitting a functional graph into cycles and the tails flowing into them: peel off nodes with no incoming edges to find the cycles, dfs the tails backwards from each cycle node for entry node, distance and tin / tout, then answer a -> b by cases (same cycle, tail into tail, tail into cycle)
Reroot DP
Reroot DP (LeetCode 3241, Time Taken to Mark All Nodes)
Worked example of reroot DP for the farthest node from every node: build a children list, one dfs for the longest and second longest way down through different children, then a second dfs that records a node's answer as soon as it arrives and hands each child the longest way up through its parent
Sliding Window on Tree
Sliding window on tree (LeetCode 3425, Longest Special Path)
Worked example of a sliding window over the root-to-node path during a dfs: arriving at a node moves the window's start past the last copy of its value, and leaving restores the start and the value's last index from a snapshot
Golden References — LeetGoat