PythiloLesson 14 of 32 · progress kept in this browser

Trade memory for speed with a dictionary

Checking every pair of numbers is quadratic. A dictionary remembers values you have already seen and answers "have I seen x?" in constant time on average, so one pass is enough.

seen = {}
for index, value in enumerate([2, 7, 11]):
    seen[value] = index
print(9 - 7 in seen)   # True
Your task

Write two_sum(items, target) returning indices (i, j) with i < j and items[i] + items[j] == target, choosing the pair whose j is smallest. Return None when no pair exists. The list is not sorted.