Accel Thinking

Python Learning Notes

What Does Big O Mean? Think About Growth Before Memorizing a Table

Understand O(1), O(N), and O(N²) by counting how work grows, then compare that growth with the input constraints.

Beginners often treat O(1), O(N), and O(N²) as formulas to memorize. Big O answers a more useful question: as the input size grows, how does the amount of work grow? It describes a growth pattern, not an exact stopwatch time.

Three Growth Patterns

Input size N O(1) O(N) O(N²)
10 1 10 100
100 1 100 10,000
1,000 1 1,000 1,000,000

Constant work stays roughly unchanged. Linear work grows in proportion to N. Quadratic work grows as N × N, so increasing N by 100 can increase the work by 10,000.

O(1): Fixed Work

distance = abs(a - c) + abs(b - d)

This expression performs a fixed number of arithmetic operations regardless of the coordinate values. The CCC 2017 J3 Exactly Electrical decision is O(1). Constant time does not mean one line; it means the operation count does not grow with N.

O(N): One Pass

total = 0
for value in values:
    total += value

A list of N items causes N loop iterations. Two consecutive scans cost N + N = 2N, which is still O(N) because Big O ignores fixed multipliers and keeps the growth shape.

O(N²): Work for Every Pair

for i in range(n):
    for j in range(n):
        compare(values[i], values[j])

The outer loop runs N times and starts an N-step inner loop each time, for about N² operations. Comparing only distinct pairs gives N(N-1)/2; its leading growth is still quadratic.

Do not classify code by indentation alone. Consecutive loops add, nested ranges often multiply, and library calls such as sorting contain their own algorithms.

Constraints Turn Big O into a Decision

An O(N³) solution may be completely reasonable at N≤100, as in USACO Daisy Chains. At N=100,000, an O(N²) solution implies roughly ten billion pair checks and is usually unacceptable. Language, constants, operation cost, and time limits still matter, so the usual limits table is a screening heuristic rather than a guarantee.

Use this workflow:

  1. Define what N measures.
  2. Identify which work grows with N .
  3. Add consecutive work and multiply genuinely nested work.
  4. Keep the dominant growth term.
  5. Estimate the count at the maximum allowed N .

Common Misunderstandings

  1. Big O is an exact running time. It is not; it describes growth.
  2. Any two loops imply O(N²) . Consecutive loops remain linear.
  3. Multiplying a number by N costs O(N) . One arithmetic multiplication is fixed work in this model.
  4. A lower Big O automatically makes code better. Correctness, clarity, and actual constraints still matter.
  5. O(N²) is always too slow. It can be ideal when N is small.

Practice

  1. What is the Big O of 3N+5 operations?
  2. An outer loop runs N times and its inner body always runs 10 times. What is the complexity?
  3. Compute N³ at N=100 . Why can some problems accept it?
  4. What is the complexity of sorting and then scanning once?
  5. Take an old solution and define its N and worst-case order of growth.

One Sentence to Remember

Big O describes how work grows with input size: count the work, read the constraints, and then decide whether the algorithm is fast enough.