cheapest(fares, k) returning the k smallest numbers from a list using heapq.nsmallest — the right tool when k is small relative to a large list, beating a full sort. Mention the complexity difference in the discussion: O(n log k) vs O(n log n).cheapest([4500, 3200, 7800, 2900], 2)
[2900, 3200]
The two lowest fares in ascending order.
import heapq.
nsmallest(k, iterable) does everything.