Glossary#
| Term | Plain meaning | Chapter |
|---|---|---|
| input size (n) | how big the input is | 1 |
| basic step | an operation whose cost doesn't depend on n | 1 |
| Big-O | upper bound on growth, ignoring constants and smaller terms | 2 |
| constant / log / linear / linearithmic / quadratic / exponential / factorial | 1 / log n / n / n log n / n² / 2ⁿ / n! | 2 |
| logarithm (log₂ n) | how many halvings from n reach 1 | 3 |
| hidden loop | a method call that itself loops (contains, substring, +) | 4 |
| time–space trade-off | using memory to save time (or the reverse) | 4 |
| worst / best / average case | max / min / expected steps for size n | 5 |
| Ω / Θ | lower bound / tight bound | 5 |
| space complexity / auxiliary space | memory growth / extra memory beyond input | 6 |
| in-place | O(1) auxiliary space | 6 |
| amortized cost | average per operation over a sequence | 7 |
| geometric growth | capacity multiplied by a factor on each resize | 7 |
| recursion tree / recurrence | calls drawn as a tree / cost written as a formula | 8 |
| memoization | caching results of subproblems | 8 |
| divide and conquer | split, solve parts recursively, combine | 8 |
| Master Theorem | general solution for divide-and-conquer recurrences | 8 |
| constraints | stated limits on input size | 10 |
| JIT warm-up / JMH | early runs are slower / Java's benchmarking tool | 11 |
Cheat Sheet#
Ladder (n = 10⁶)
O(1) 1 · O(log n) 20 · O(n) 10⁶ · O(n log n) 2×10⁷ · O(n²) 10¹² ✗ · O(2ⁿ) ✗ · O(n!) ✗
doubling n: O(n) ×2 · O(n log n) a bit over ×2 · O(n²) ×4 · O(n³) ×8Rules
sequence → add, keep the biggest nested → multiply i..n inner loop → n²/2 → O(n²)
halve/double each round → log n separate inputs → O(n + m) / O(n · m)
count every call's own cost early return ≠ better worst caseRecurrences
T(n/2)+1 → log n T(n−1)+1 → n T(n−1)+n → n²
2T(n/2)+n → n log n 2T(n/2)+1 → n 2T(n−1)+1 → 2ⁿ b branches, depth d → bᵈConstraints → allowed complexity (≈10⁸ ops/s)
≤ 10: n! · ≤ 20: 2ⁿ · ≤ 500: n³ · ≤ 5,000: n² · ≤ 10⁶: n log n / n · ≤ 10⁸: n · huge: log n / 1Java costs to never forget
ArrayList add-at-end amortized O(1) · add(0)/remove(0) O(n) · contains O(n)
HashMap/HashSet O(1) avg · TreeMap/TreeSet O(log n) · PriorityQueue offer/poll O(log n), peek O(1)
ArrayDeque both ends O(1) · String + / substring / equals O(n) · sort O(n log n)The answer template
"Time O(__) because __ dominates. Space O(__) for __. (Average case because of hashing.)
Trade-off: could get O(__) time with O(__) space by __."Try This Yourself#
Reproduce Part 0. Write the three duplicate checkers and time them for n = 10k, 20k, 40k, 80k (run each a few times first to warm up the JIT). Check the ratios: ×4 for the pairs version, about ×2 for the others.
Analyse without running. For each loop in Chapter 4's Check Yourself, add a counter, run it for n = 1,000 and 2,000, and confirm the growth matches your answer.
Feel the explosion. Time naïve
fib(n)for n = 30, 35, 40, 42. Predict each time from the previous before running. Then write the bottom-up O(1)-space version and timefib(90).Constraint drill. Open any 5 LeetCode problems, read only the constraints, and write down the complexity you'd target before reading the problem statement.