LeetCode reference system
Turn problem signals into working code.
Build fluency with the essential patterns first: arrays, search, trees, graphs, and dynamic programming in contest-ready C++17 and Python 3.
- 0templates
- 2languages
- 27core patterns
# Read constraints first
signal = "shortest path"
weights = "nonnegative"
if graph.is_unweighted():
algorithm = "BFS"
else:
algorithm = "Dijkstra"
assert complexity_fits()
write_invariant()
implement_and_verify()
01 · Thinking flow
One loop for every problem
Move left to right. If you cannot state the invariant, pause before coding.
- 01
Restate
Inputs, output, validity, mutation.
- 02
Bound
Derive complexity from constraints.
- 03
Shape
Sequence, tree, graph, grid, state.
- 04
Bottleneck
Which repeated operation is expensive?
- 05
Pattern
Match the signal and write the invariant.
- 06
Structure
Make the bottleneck operation cheap.
- 07
Prove
Trace a tiny case and boundaries.
- 08
Ship
Implement, test, audit complexity.
02 · Algorithm decision graph
Start with the shape. Follow the constraint.
Select the input shape to reveal the questions that narrow the solution to the right family.
Is the answer contiguous, sorted, or a repeated range aggregate?
03 · Pattern selector
Follow the strongest signal
Choose the algorithm from the required fast operation—not from the story wrapped around the problem.
04 · Complexity compass
Let constraints eliminate bad ideas
Backtracking, bitmask DP
Interval DP, Floyd–Warshall
2D DP, pair enumeration
Sort, heap, divide-and-conquer
Hashing, prefix, two pointers
Binary search on answer
05 · Template library
Search. Select. Copy. Adapt.
Each template includes its trigger, target complexity, invariant, and paired implementation.
No template matches yet.
Try a broader signal such as “range”, “graph”, “minimum”, or “count”.
06 · Before submit
Seven checks between “works” and “accepted”
- 01I can state the invariant.
- 02Complexity fits the maximum constraints.
- 03All ranges and bounds are intentional.
- 04Duplicates and visited states are handled once.
- 05Sums, products, and distances cannot overflow.
- 06Mutation and copied paths are deliberate.
- 07A tiny hand trace matches the code.