Choose the Earliest Shipments
Context
A warehouse dispatch team tracks shipments as two-number records: the first number is the scheduled delivery time, and the second identifies the shipment. They need a dispatch list containing at most a requested number of shipments, starting with the earliest deliveries.
Problem
Write a function that receives shipment records and a requested limit. Each record has exactly two integers: delivery time first and shipment identifier second. Order all records by delivery time from smallest to largest, then return the first requested number of complete records. If several records have the same delivery time, keep them in their original input order. Do not remove duplicate records, and return an empty list when the requested limit is zero or there are no shipments.
Examples
limit = 3shipments = [[5, 101], [2, 102], [8, 103], [2, 104]][[2, 102], [2, 104], [5, 101]]limit = 1shipments = [[10, 501], [4, 502], [7, 503]][[4, 502]]limit = 4shipments = [[3, 201], [3, 201], [1, 202], [6, 203]][[1, 202], [3, 201], [3, 201], [6, 203]]Constraints
0 <= len(shipments) <= 1002 <= len(shipments[i]) <= 2 for every i0 <= shipments[i][j] <= 1000000 for every i, jshipments: rectangular0 <= limit <= len(shipments)- Types:
shipmentsis int[][],limitis int; result is int[][]
Function signature
def choose_earliest_shipments(shipments: list[list[int]], limit: int) -> list[list[int]]
shipmentslist[list[int]]- Shipment records, each containing delivery time followed by shipment identifier.
limitint- Maximum number of shipments to include in the dispatch list.
- returns list[list[int]]
- The selected complete shipment records, ordered by nondecreasing delivery time and containing no more than limit records.
Adapted from MBPP problem task_753 (CC BY 4.0). Rewritten, extended and verified by Iksha.
Notes
- The first number in every record is the ranking key.
- Equal ranking keys retain their input order.