KatInterview

Courier Pay with Overlap Multipliers, Waits and Peak Windows

Greedy & IntervalsAuto-graded · 20 test cases
MediumAuto-gradedGreedy & Intervalssweep-lineintervalsevent-sorting

Last updated

Sweep-line over time intervals: pay grows with the square of concurrent deliveries, store-wait minutes are isolated, and peak windows double the rate.

Courier Pay Calculator

A delivery platform pays couriers per minute of work. Time is measured in whole minutes, and every time range is half-open: a range [s, e) covers minutes s, s+1, ..., e-1 and does not include minute e. A range with s >= e is empty and contributes nothing.

Input

  • orders: a list of dictionaries. Each has:
    • "accepted_at" and "delivered_at" (integers), always present.
    • "arrived_at" and "picked_up_at" (integers), optional.
  • rate: an integer, the base pay per minute.
  • peak_intervals: a list of [start, end] pairs describing peak windows, each read as the half-open range [start, end).

Pay rules

Shared minutes. For every minute, let k be the number of orders that are shared-active in that minute. Each shared-active order earns rate * k for that minute, so the minute pays k * k * rate in total.

Which minutes are shared-active.

  • If an order lacks "arrived_at" or lacks "picked_up_at" (or both), it has no store wait. It is shared-active on [accepted_at, delivered_at).
  • If an order has both "arrived_at" and "picked_up_at", it is split into three pieces:
    • [accepted_at, arrived_at) is shared-active.
    • [picked_up_at, delivered_at) is shared-active.
    • [arrived_at, picked_up_at) is the store-wait piece. It is not shared-active, so it does not count toward k for any order (including itself). Each of its minutes pays exactly rate, independent of every other order.
  • Use the three pieces exactly as the given numbers define them. Any piece with start >= end is empty and ignored. Do not clip pieces to the order's other timestamps.

Peak windows. A minute is a peak minute if it lies in at least one of the peak intervals. Overlapping or touching peak intervals form a union, and a minute covered by several of them is still just one peak minute. Everything paid for a peak minute is doubled, both shared pay (2 * k * k * rate) and store-wait pay (2 * rate). Peak intervals with start >= end are ignored.

Task

Return the total pay (an integer) summed over all minutes and all orders.

If orders is empty, the answer is 0.

Constraints

  • Timestamps are integers and can be large, so iterating minute by minute is not a good idea.
  • rate is a non-negative integer.
  • The number of orders and the number of peak intervals are each modest enough for an O((N + P) log (N + P)) solution, plus a pass over the orders for the waits.
  • Back-to-back ranges (one ends at minute t, another starts at t) do not overlap.
  • Several orders may share identical timestamps.

Examples

Example 1

Input: compute_courier_pay([{'accepted_at': 0, 'delivered_at': 3}, {'accepted_at': 1, 'delivered_at': 4}], 2, [])

Output: 20

Example 2

Input: compute_courier_pay([{'accepted_at': 0, 'arrived_at': 1, 'picked_up_at': 3, 'delivered_at': 4}, {'accepted_at': 1, 'delivered_at': 3}], 1, [[2, 4]])

Output: 9

Hints

Hint 1

The total only depends on how many shared-active orders there are at each minute, not on which ones. Between two consecutive 'interesting' timestamps that count is constant.

Hint 2

Turn every shared-active piece into a +1 event at its start and a -1 event at its end, and add +1/-1 events for the merged peak windows too. Sort the distinct timestamps and charge each gap as length * rate * k * k * (2 if inside a peak else 1), applying the deltas after charging.

Hint 3

Keep store-wait pieces out of the sweep entirely and pay them separately at multiplier 1. For each one, add rate times (its length plus the number of its minutes that fall inside the merged peak union).