Code Logo

Earliest Pickup Times with heapq()

Published at22 Apr 2026
Python Data Structures Easy 0 views
Like0

A delivery desk collects possible pickup times in minutes. The manager wants to quickly see the earliest two times so the first drivers can be assigned without scanning the full list by hand.

Your task is to return a list containing the two smallest values in ascending order. If the input has only one time, return a one-item list containing that value. If the input is empty, return an empty list.

For example, if the times are [18, 7, 12, 5], the answer should be [5, 7] because those are the earliest two pickup times. If the input is [9], the answer should simply be [9].

This challenge is a small introduction to priority-queue thinking. Instead of sorting the entire list just to find a few smallest values, Python offers heap tools that focus directly on the lowest-priority items.

Example Input & Output

Example 1
Input
times = [18, 7, 12, 5]
Output
[5, 7]
Explanation

The answer contains the two smallest times in ascending order.

Example 2
Input
times = []
Output
[]
Explanation

No pickup times means there is nothing to return.

Example 3
Input
times = [9]
Output
[9]
Explanation

A one-item input returns that one time inside a list.

Algorithm Flow

Recommendation Algorithm Flow for Earliest Pickup Times with heapq()
Recommendation Algorithm Flow for Earliest Pickup Times with heapq()

Solution Approach

This problem is a good match for heapq because a heap is designed to surface the smallest item quickly. When you need only the first few smallest values, that data structure expresses the intent very clearly.

Python gives you two neat options. The shortest one is heapq.nsmallest(2, times), which directly asks for the two smallest values and returns them in ascending order:

import heapq

return heapq.nsmallest(2, times)

That already handles all three cases the problem cares about. If the list is empty, the result is []. If there is one element, the result is a one-item list. If there are two or more, you get the smallest two.

You can also make the heap behavior more explicit by copying the list, applying heapify(), and then popping from the heap up to two times. That version shows how a heap turns the collection into a structure where the smallest value is always available at the top.

The key data-structure lesson is that heapq is useful when you care about repeated access to the smallest items, not necessarily about fully sorting everything first. For a tiny problem like this, a full sort would still work, but the heap-based solution better matches the actual goal.

The runtime with nsmallest is efficient for small requested counts, and the code stays very direct. That makes this a friendly first challenge for Python's built-in heap tools.

Best Answers

python - Approach 1
import heapq

def earliest_pickups(times):
    heap = times[:]
    heapq.heapify(heap)
    result = []
    for _ in range(min(2, len(heap))):
        result.append(heapq.heappop(heap))
    return result