KatInterview

Courier Earnings from Overlapping Order Timelines

Greedy & IntervalsAuto-graded · 19 test cases
MediumAuto-gradedGreedy & Intervalssweep-linesortingintervals

Last 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]:

  • time is an integer number of minutes (for example, minutes since midnight).
  • order_id is a string identifying the order.
  • action is either "ACCEPT" or "FULFILL".

Output

Return the total earnings in dollars as a float, rounded to two decimal places.

Processing rules

  1. The log may be in any order. Process events in increasing order of time.
  2. 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.
  3. Processing an event updates the open set:
    • "ACCEPT" adds order_id to the open set. If it is already in the set, nothing changes (an order is never counted twice).
    • "FULFILL" removes order_id from the open set. If it is not in the set, nothing changes.
  4. 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.
  5. Let t and t_next be 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 time t has been processed. The span earns open_count * (t_next - t) * 0.30.
  6. 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.
  7. Spans with zero open orders earn nothing. An empty log, or a log with a single distinct timestamp, earns 0.0.
  8. Return the sum of all span earnings, rounded to two decimals.

Constraints

  • 0 <= len(events)
  • time is an integer.
  • order_id is a string.
  • action is 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.