Lesson 3 of 27Read before you code
Constraints Are Hints
Turn the constraints section into a time budget, then use value ranges and promises to rule algorithms in and out.
The constraints section at the bottom of a problem looks like small print. It is actually the most generous hint you will get. The input size tells you how fast your solution must be, the value ranges tell you which data structures will work, and the promises ("sorted", "distinct", "exactly one answer") tell you which cases you can skip. Strong solvers read the constraints before they think about an algorithm.
The budget: about 10^8 simple operations
A typical judge gives your code one or two seconds. A compiled language does roughly 10^8 simple operations per second (an addition, a comparison, an array read). Python is slower, closer to 10^7 per second for plain loops, so treat Python budgets with some care.
That gives you a working rule: plug the largest n into your complexity, and the result should be at most about 10^8. If it is 10^10, the solution will time out, however clean it is.
Press Run (⌘ Enter) to see what happens.10^5 squared is 10^10, a hundred times over budget. 10^5 times 17 is under two million. That single calculation tells you a nested loop is out and a sort or a single pass is in.
The constraint table
Memorise this table. It turns the first line of the constraints into a target complexity in two seconds.
| Largest n | Affordable complexity | Typical approach |
|---|---|---|
| n ≤ 10 | O(n!) | try every ordering (permutations) |
| n ≤ 20 | O(2ⁿ) or O(n · 2ⁿ) | try every subset, bitmasks, backtracking |
| n ≤ 500 | O(n³) | triple loop, interval DP, Floyd-Warshall |
| n ≤ 5000 | O(n²) | double loop, 2D DP table |
| n ≤ 10^6 | O(n log n) or O(n) | sort, heap, binary search, hash map, one pass |
| n larger (10^9, 10^18) | O(log n) or O(1) | binary search on the answer, math formula |
Why these cut-offs? Plug them in: 10! is about 3.6 million. 2^20 is about a million (and 20 · 2^20 is about 2 · 10^7). 500³ is 1.25 · 10^8. 5000² is 2.5 · 10^7. All sit near the budget. For n up to 10^9 you cannot even read that many numbers in time, so the answer must come from logarithms or arithmetic.
Drag the slider and watch 2ⁿ and n! leave the chart. Exponential and factorial algorithms are fine at n = 10 and hopeless at n = 50. That is why a problem with n <= 20 is almost shouting "try every subset".
1 <= nums.length <= 10^5. What should you aim for?Match the input size to the complexity it allows.
Is an O(n³) loop safe for n = 500? Check the count.
n = 500 print(n ** 3)
Two sizes, multiply them
Grids and pairs of strings have two sizes. Multiply them. 1 <= m, n <= 200 means 40,000 cells, so O(m · n) is trivial and even O(m · n · 4) for four directions is fine. Two strings of length up to 1000 (Longest Common Subsequence, Edit Distance) give a 10^6-cell table: O(m · n) DP is exactly what is expected.
Watch for a third number too. Problems often say 1 <= k <= n or give the number of queries q. If there are 10^5 queries on an array of 10^5, each query must be O(log n) or O(1), which points at prefix sums or a precomputed structure.
Value ranges
The second line of constraints is usually about the values. It decides which structures you can use.
| Constraint | What it tells you |
|---|---|
-10^9 <= nums[i] <= 10^9 | Values are huge and can be negative. A counting array indexed by value is out; use a hash map or sort. |
0 <= nums[i] <= 10^4 | A counting array of size 10^4 + 1 works and is often faster than a hash map. |
s has only lowercase English letters | A fixed array of 26 counts. O(1) space for the alphabet. |
0 <= x <= 26 or 1 <= k <= 26 | The same: a small fixed set, often a bitmask fits too. |
nums[i] > 0 (all positive) | Running sums only grow, so a sliding window on sums works. Negatives would break it. |
| Sums up to 10^5 · 10^9 | 10^14 overflows a 32-bit int. Fine in Python, use long in Java or C++, and in JS stay under 2^53. |
k' with -10^5 <= nums[i] <= 10^5. What does the value range tell you?Promises: sorted, distinct, one answer
Some lines are not limits but promises. Each one is a gift.
- "
numsis sorted": binary search (O(log n) lookups) and two pointers moving inward. If you are not using the order, you are probably missing the intended solution. - "All values are distinct": no duplicate handling. In Subsets or Permutations, you do not need to skip repeats.
- "Exactly one valid answer" or "at least one answer exists": you can return the first one you find, and you do not have to code a "not found" branch.
- "The answer fits in a 32-bit integer": you do not need big-number handling, and it often hints the answer is built by multiplying or summing.
- "Must run in O(log n)" or "O(1) extra space": the target is handed to you. O(log n) on an array means binary search. O(1) space means pointers or reusing the input.
Match the promise to what it lets you do.
Order these from the LARGEST n you can handle to the smallest, all with a roughly 10^8 budget.
- Tap the steps below in the right order.
Try it on a real problem
Contains Duplicate allows up to 10^5 numbers that fit in a 32-bit integer. Read the constraints first: the size rules out comparing every pair, and the value range rules out a counting array. What is left is a set, or sorting.
Contains Duplicate
You are given a list of integers nums. Return true if any value shows up at least twice, and false if every value is distinct.
nums = [1, 2, 3, 1] → true (1 appears twice)
nums = [1, 2, 3, 4] → false
nums = [5, 5, 5] → trueConstraints
0 <= nums.length <= 10^5-10^9 <= nums[i] <= 10^9
- operation budget
- About 10^8 simple operations per second in a compiled language (less in Python). Plug the largest n into your complexity and stay under it.
- constraint table
- n ≤ 10 → n!, ≤ 20 → 2ⁿ, ≤ 500 → n³, ≤ 5000 → n², ≤ 10^6 → n log n or n, larger → log n or O(1).
- counting array
- An array indexed by value. Only works when values are small and non-negative, like 0 to 10^4 or 26 letters.
- promise
- A constraint that removes work: sorted, distinct, exactly one answer, fits in 32 bits.
- overflow
- A result bigger than the integer type can hold. Python is safe; Java, C++ and JS (past 2^53) are not.