Python 3D Academyবাংলা

Reading the limits: input size → the speed you need

🌍 In real life. A courier company promises to answer "where is my parcel?" in under a second. Before writing a line of code, its engineers look at the numbers: 2,000 parcels a day is a small job that any method handles; 5,000,000 parcels means the slow, simple idea will time out, however tidy the code is. Problem statements give you the same numbers: the limits under the examples, like "1 ≤ n ≤ 100,000".

The idea in one sentence: Python does roughly 10,000,000 simple steps a second, so put the biggest n into each Big-O and keep only the ones that stay under that budget: the limit tells you how fast your solution must be before you choose a technique.

Its words:

  • constraint (limit): the largest inputs the problem promises, such as n ≤ 100,000
  • step budget: about 10,000,000 simple steps a second in Python
  • time limit: usually 1–2 seconds

What fits, by size (about 10,000,000 steps):

  • n ≤ 10: O(n!) (10! is 3,628,800): try every order
  • n ≤ 20: O(2ⁿ) (about 1,000,000): try every subset
  • n ≤ 200: O(n³) (8,000,000): three nested loops
  • n ≤ 3,000: O(n²) (9,000,000): every pair
  • n ≤ 500,000: O(n log n) (about 9,500,000): sort, heap, binary search
  • n ≤ 10,000,000: O(n): one pass with a hash map, two pointers or a window
  • bigger, like 10¹⁸: O(log n) or a formula

Counting the steps of two nested loops shows how n² grows:

And a race between the growth rates (Chapter 33) shows why the budget table falls off so steeply at the slow end:

Why reading the limits works. The judge runs your code on the biggest allowed input, so the worst case is what counts. Put that n into each Big-O: with n = 100,000, O(n²) is 10,000,000,000 steps, about 1,000 seconds, while O(n log n) is about 1,700,000 steps, a fraction of a second. Only one of them can pass, whatever the code looks like.

What it costs, by counting. This check is free: a multiplication in your head before you start. Skipping it costs the most: writing an O(n²) solution, watching it time out, and starting again.

import math

BUDGET = 10_000_000                     # roughly what Python does in a second

def steps(kind, n):
    if kind == "n^2":
        return n * n
    if kind == "n log n":
        return n * math.log2(n)
    return n

for n in [3000, 100000, 1000000]:
    fits = [kind for kind in ["n^2", "n log n", "n"] if steps(kind, n) <= BUDGET]
    print(n, fits)

Mistakes and edge cases.

  • using the example's size: the examples have 5 numbers; the limits say 100,000, and that is the case that times out
  • forgetting the queries: q questions, each O(n), cost O(n × q); 100,000 of each is 10,000,000,000 steps
  • constant factors: Python is slower than C++, so a borderline O(n²) at n = 10,000 (100,000,000 steps) will usually fail
  • memory limits too: a 10,000 × 10,000 table holds 100,000,000 cells, far too many

Quiz

  1. n can be 100,000. Which speed do you need?

    • O(n log n) or better
    • O(n²) is fine
    • O(2ⁿ)
  2. n is at most 15. Is trying every subset fast enough?

    • Yes: 2 to the power 15 is only 32,768 subsets
    • No: subsets are always too slow
    • Only if the list is sorted
  3. What does this print?

    print(3000 ** 2 <= 10_000_000, 4000 ** 2 <= 10_000_000)
  4. 100,000 numbers and 100,000 questions "what is the sum from i to j?". What is wrong with adding up each range?

    • Up to 10,000,000,000 steps; prefix sums answer each question in O(1)
    • Nothing: each sum is fast
    • It needs too much memory
  5. n can be 100,000. This gives the right answers, but it is far too slow. Which line makes it O(n²)?

    def has_duplicate(nums):
        for i in range(len(nums)):
            if nums[i] in nums[i + 1:]:
                return True
        return False

Coding tasks

  1. Write best_speed(n): using the table above, return the slowest Big-O that still fits for this n, as one of the strings "O(n!)", "O(2^n)", "O(n^3)", "O(n^2)", "O(n log n)", "O(n)" or "O(log n)". A limit is included: n = 20 still allows O(2^n).

  2. Write count_pairs_steps(n): how many times the inner line runs in for i in range(n): for j in range(i + 1, n): ..., without running the loops.