Trade time for space: hash maps and sets turn repeated "have I seen this?" scans into O(1) lookups, and prefix sums turn range questions into subtraction.
crash course ↗Two indices walking a sorted or symmetric array — converging from the ends or chasing each other — collapse an O(n²) pair search into a single O(n) sweep with O(1) space.
crash course ↗A grow-right / shrink-left window over a contiguous subarray or substring answers "best/longest/shortest run satisfying a condition" in one O(n) pass.
crash course ↗LIFO bookkeeping: match pairs, defer work until you can resolve it, and use the monotonic-stack trick to find the next greater/smaller element in O(n).
crash course ↗Halve a sorted — or monotonically decidable — search space each step. The hard part is rarely the array; it is spotting the monotone predicate to bisect.
crash course ↗Strings are arrays of characters with their own toolkit: frequency counts, two pointers, sliding windows, and careful index arithmetic.
crash course ↗Treat integers as bit vectors: XOR cancels pairs, AND masks, shifts move, and n & (n-1) clears the lowest set bit — O(1)-ish tricks that replace whole loops.
crash course ↗Define a state, write a recurrence over smaller subproblems, then memoize or tabulate. Most DP is recognizing the state, not the code.
crash course ↗Build a candidate incrementally, recurse, then undo the last choice. The template is fixed; the art is the choices, the prune, and the base case.
crash course ↗Make the locally optimal choice and never reconsider. Fast and short — but only correct when an exchange argument proves the greedy choice is safe.
crash course ↗Model the problem as nodes and edges, then pick the traversal: BFS for shortest unweighted paths and levels, DFS for connectivity and cycles, union-find for grouping, topo sort for ordering.
crash course ↗Almost every interval problem starts by sorting on start (or end), then making a single sweep that merges, counts overlaps, or greedily keeps the earliest finisher.
crash course ↗Pointer surgery without random access: dummy heads tame edge cases, fast/slow pointers find middles and cycles, and reversal is the recurring sub-move.
crash course ↗A grid is a graph in disguise. Master index↔coordinate math, in-place transforms (transpose + reverse), and 4-directional BFS/DFS flood fills.
crash course ↗Pattern-spot the closed form: digit manipulation, modular cycles, fast exponentiation, and coordinate tricks that sidestep brute force.
crash course ↗Recursion is the native language of trees: answer the question for the children, then combine. DFS for structure, BFS for levels, in-order for BST sortedness.
crash course ↗A prefix tree turns a set of words into a character-by-character path graph, making prefix queries, autocomplete, and word-search backtracking efficient.
crash course ↗When you repeatedly need the current min or max, a heap gives O(log n) push/pop. Top-K, merging k streams, and running medians all lean on it.
crash course ↗Precompute a running total so any range answer becomes a single subtraction; pair the prefix with a hash map of seen sums to answer subarray questions in O(n) — even with negatives, where windows fail.
technique ↗Keep a stack or deque whose values stay sorted; the element that breaks the order triggers pops, and each pop resolves an answer — next greater/smaller, the bounding wall, the window extreme — in O(n) amortized.
technique ↗Two pointers at different speeds over a sequence: the hare laps the tortoise inside any cycle (Floyd), the slow one lands on the middle, and a reset finds the loop's entrance — all in O(1) space.
technique ↗Stop searching the array and bisect the answer space instead: find the smallest/largest value where a monotone predicate feasible(x) flips. The whole skill is spotting the predicate and writing feasible().
technique ↗Maintain a forest where each element points toward a representative root; find with path compression and union by rank answer "are these connected?" and "how many groups?" online in near-O(1) amortized.
technique ↗Seed the BFS queue with every source at distance 0 at once, then expand in layers — the first time any cell is reached gives its distance to the nearest source, all in one O(V+E) sweep.
technique ↗