🐐
LeetGoat
Interview Prep
Competitive Programming
Discord
Misc. templates
Range queries
2D range queries
Sets
Union find
Strings
Tree queries
Tree helpers
Graphs
Matrices
Math
Primes
Bit ops & Bitset
Geometry
Misc.
Golden References
Hash Tables
Hash set (C++) O(1)
hash_set<K>: about 3x faster than an unhackable std::unordered_set. Keys: any integer type, a pair of integers, or string
Hash map (C++) O(1)
hash_map<K, V>: about 3x faster than an unhackable std::unordered_map. Keys: any integer type, a pair of integers, or string; values: anything
Stacks
Monotonic stack O(n)
For every index, the nearest index to its left or right with a value <, <=, > or >= it
Bisects
Bisects O(log n) or O(1)
First / last index with a value <, <=, >, >= a threshold, for increasing and for decreasing sorted arrays
Mo's
Mo's algorithm (Hilbert order)
Offline range queries by sliding a window: sort the queries by hilbertOrder, then fill in addL / addR / remL / remR
3D Mo's (with updates)
Offline range queries with point updates mixed in: paste the block inside your function, fill in the input, state, hooks and answerQuery
Longest Increasing Subsequence
Longest increasing subsequence O(n log n)
Strictly / non-strictly increasing and decreasing: each returns (length, one longest subsequence)
Next Permutation
Next permutation O(n)
In place: O(1) amortized if values are distinct enough
Previous permutation O(n)
In place: O(1) amortized if values are distinct enough
Next permutation, steps ahead O(n log n)
Needs steps >= 0. O(n log n) while steps fits in 64 bits; a far larger steps in Python also pays for big integers
Bubble Sort
Min swaps to sort O(n log n)
Min swaps to reach a target state O(n log n)
Min swaps to a palindrome O(n log n)
Chess
Knight distance O(1)
Fewest knight moves between two squares: on an infinite board, or from the corner of a board that's infinite in one quarter
Misc. Templates — LeetGoat