Courier Earnings from Overlapping Order Timelines
Greedy & IntervalsAuto-graded · 19 test casesLast updated
Sweep over unsorted accept/fulfill events to total a courier's earnings, where pay per minute scales with the number of simultaneously open orders.
Courier Earnings with Concurrent Orders
A delivery courier takes on several orders during a shift. You get a log of events recording when orders were accepted and when they were completed. Compute the courier's total earnings.
Pay rules
- At any moment, the open set is the set of order ids that have been accepted and not yet fulfilled.
- Each minute pays $0.30 multiplied by the number of orders in the open set during that minute. Two open orders pay $0.60 per minute, three pay $0.90, and so on.
Input
events is a list of entries [time, order_id, action]:
timeis an integer number of minutes (for example, minutes since midnight).order_idis a string identifying the order.actionis either"ACCEPT"or"FULFILL".
Output
Return the total earnings in dollars as a float, rounded to two decimal places.
Processing rules
- The log may be in any order. Process events in increasing order of
time. - When several events share the same
time, process every"ACCEPT"at that time before any"FULFILL"at that time. Events of the same kind at the same time can be processed in any order. - Processing an event updates the open set:
"ACCEPT"addsorder_idto the open set. If it is already in the set, nothing changes (an order is never counted twice)."FULFILL"removesorder_idfrom the open set. If it is not in the set, nothing changes.
- The open count is the number of distinct ids in the open set, so an id can be reopened by a later
"ACCEPT"after it has been fulfilled. - Let
tandt_nextbe two consecutive distinct event times. Over the span[t, t_next), the open count is the size of the open set after every event at timethas been processed. The span earnsopen_count * (t_next - t) * 0.30. - Time after the last event timestamp earns nothing, so an order that is never fulfilled counts only up to the last timestamp in the log.
- Spans with zero open orders earn nothing. An empty log, or a log with a single distinct timestamp, earns
0.0. - Return the sum of all span earnings, rounded to two decimals.
Constraints
0 <= len(events)timeis an integer.order_idis a string.actionis always"ACCEPT"or"FULFILL".
Examples
Example 1
Input: courier_earnings([[375, 'A', 'ACCEPT'], [378, 'B', 'ACCEPT'], [396, 'A', 'FULFILL'], [405, 'B', 'FULFILL']])
Output: 14.4
Example 2
Input: courier_earnings([[396, 'A', 'FULFILL'], [375, 'A', 'ACCEPT'], [405, 'B', 'FULFILL'], [378, 'B', 'ACCEPT']])
Output: 14.4
Example 3
Input: courier_earnings([[0, 'A', 'ACCEPT'], [0, 'B', 'ACCEPT'], [5, 'A', 'FULFILL'], [5, 'B', 'FULFILL']])
Output: 3
Hints
Hint 1
The count of open orders only changes at event timestamps, so you never need to walk minute by minute. Jump from one timestamp to the next.
Hint 2
Sort by (time, accept-first). Then, for each distinct timestamp, apply all its events to a set of open order ids, and charge the span up to the next timestamp using the size of that set.
Hint 3
Accumulate an integer total of 'order-minutes' (open count times span length) and convert to dollars only once at the end, which avoids floating-point drift.
Worked solution
Medium Try it yourself first — then compare your approach, complexity and edge cases.
Approach
This is a sweep over time.
- Sort the events by time. When times tie, put
ACCEPTbeforeFULFILL. - Walk through the sorted list one timestamp group at a time. For each group:
- Apply every event in the group to a set of open order ids (add on
ACCEPT, discard onFULFILL). Adding an id already present or discarding an id not present changes nothing. - If another timestamp follows, add
len(open_orders) * (next_time - current_time)to a running integer total of order-minutes.
- Apply every event in the group to a set of open order ids (add on
- Multiply the total by 0.30 once at the end and round to two decimals.
Why it works
- The open set only changes at event timestamps. Between two consecutive distinct timestamps the count is constant, so one multiplication gives that span's contribution exactly.
- Applying all events at a timestamp before charging the following span matches the rule that the count used is the one after all actions at
t. - Processing
ACCEPTbeforeFULFILLat equal times keeps the set consistent. An order accepted and fulfilled in the same minute is added and then removed, and the zero-length span between them earns nothing. - Using a set means each id counts at most once, and stray or repeated events are harmless no-ops.
- Keeping the sum as an integer and converting once at the end avoids the floating-point error that repeated addition of 0.3 would accumulate. Since 0.30 times an integer has at most one decimal digit, rounding to two decimals is exact.
- An order that is never fulfilled only counts up to the last event timestamp, because no span follows the final group. An empty log returns
0.0.
Complexity
- Time: O(n log n) for the sort, then O(n) for the sweep.
- Space: O(n) for the sorted copy and the set of open orders.
Reference solution
from typing import List
def courier_earnings(events: List[list]) -> float:
if not events:
return 0.0
# Sort by time; at equal times, ACCEPT comes before FULFILL.
ordered = sorted(events, key=lambda e: (e[0], e[2] == "FULFILL"))
open_orders = set()
order_minutes = 0 # integer sum of (open count * span length)
i = 0
n = len(ordered)
while i < n:
current = ordered[i][0]
# Apply every event sharing this timestamp.
while i < n and ordered[i][0] == current:
_, order_id, action = ordered[i]
if action == "ACCEPT":
open_orders.add(order_id)
else:
open_orders.discard(order_id)
i += 1
# Charge the span up to the next timestamp, if there is one.
if i < n:
order_minutes += len(open_orders) * (ordered[i][0] - current)
# 0.30 dollars per order-minute, converted once at the end.
return round(order_minutes * 3 / 10, 2)