A laundromat has k washing machines, all free at time 0, and one line. loads[i] = (arrival, minutes, patience), in arrival order.
Each customer, in order, looks at the machine that frees earliest and would start at start = max(arrival, free_time):
- If
start - arrival > patience, they walk out: answer-1, no machine used. - Otherwise that machine is busy until
start + minutes, and that finish time is their answer. (Waiting exactlypatienceis fine.)
Write finish_times(k: int, loads: list[tuple[int, int, int]]) -> list[int].
finish_times(2, [(0, 30, 0), (5, 10, 0), (6, 4, 5), (7, 3, 20), (40, 5, 0)])
# [30, 15, -1, 18, 45]
# customer 0: machine A, 0 -> 30
# customer 1: machine B, 5 -> 15
# customer 2: earliest machine frees at 15, a 9-minute wait > patience 5: leaves (-1)
# customer 3: waits 8 minutes (patience 20), B runs 15 -> 18
# customer 4: arrives at 40, both free: 40 -> 45
Up to 100,000 machines and 200,000 customers; the list may be empty.
⭐ Bonus: a speed test with 50,000 machines and 200,000 customers earns a star with O(log k) per customer instead of looping over every machine.
Show hint
keep a min-heap of free times, starting as [0] * k. Peek at heap[0] to decide; only if the customer stays, heapq.heapreplace it with the finish time.