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
solve.md
# 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.

  1. 01

    Restate

    Inputs, output, validity, mutation.

  2. 02

    Bound

    Derive complexity from constraints.

  3. 03

    Shape

    Sequence, tree, graph, grid, state.

  4. 04

    Bottleneck

    Which repeated operation is expensive?

  5. 05

    Pattern

    Match the signal and write the invariant.

  6. 06

    Structure

    Make the bottleneck operation cheap.

  7. 07

    Prove

    Trace a tiny case and boundaries.

  8. 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.

Start What structure carries the problem? Then identify the operation that must be fast.
Ask next

Is the answer contiguous, sorted, or a repeated range aggregate?

Candidate algorithms

03 · Pattern selector

Follow the strongest signal

Choose the algorithm from the required fast operation—not from the story wrapped around the problem.

Database or data structure? LeetCode problems nearly always need an in-memory data structure. Use a persistent database only for explicit SQL, storage, joins, or transaction tasks.
Contiguous + longest/shortestSliding windowCounter · map · deque
Many range queriesPrefix sumArray · frequency map
Next greater/smallerMonotonic stackStack of indices
Repeated min/max or top-kHeapPriority queue
Sorted values + exact targetExact binary searchInclusive bounds
First/last occurrence or insertion pointBoundary binary searchLower · upper bound
Monotone feasible answerBinary search on answerPredicate + numeric bounds
Unweighted minimum stepsBFSQueue + visited
Nonnegative weighted pathDijkstraAdjacency list + min-heap
Dependencies / prerequisitesTopological sortIndegree + queue
Repeated connectivity mergesUnion-findParent + size
Overlapping optimal statesDynamic programmingMemo · table
Lookup + recency + evictionLRU cacheHash map + doubly linked list
Insert + delete + uniform randomRandomized setArray + value-index map
Online median after every insertStreaming medianMax-heap + min-heap
Latest version at or before timeTime mapHash map + sorted history
Stack top + current minimumMin stackValue stack + min stack

04 · Complexity compass

Let constraints eliminate bad ideas

n ≤ 20O(2ⁿ)

Backtracking, bitmask DP

n ≤ 200O(n³)

Interval DP, Floyd–Warshall

n ≤ 2,000O(n²)

2D DP, pair enumeration

n ≤ 100kO(n log n)

Sort, heap, divide-and-conquer

n ≤ 1mO(n)

Hashing, prefix, two pointers

Huge valuesO(log value)

Binary search on answer

05 · Template library

Search. Select. Copy. Adapt.

Each template includes its trigger, target complexity, invariant, and paired implementation.

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.