1149. Checkout Lanes

Medium3.5 points estimatedHeapSimulationQueue

A supermarket has lanes checkout lanes numbered from 0, all empty at time 0. Customer i arrives at minute arrivals[i] (the arrivals are non-decreasing) and needs durations[i] minutes at a lane. Customers are served in arrival order. Customer i starts at the later of their arrival time and the earliest time any lane becomes free, and uses the lowest-numbered lane that is free at that start time. A lane that finishes at minute t is free for someone starting at minute t. Return an array with the minute at which each customer finishes.

1 <= lanes <= 10^5, the number of customers n satisfies 1 <= n <= 10^5, 0 <= arrivals[i] <= 10^9 and 1 <= durations[i] <= 10^4. Finish times need a long. A heap of free lanes and a heap of busy lanes give O(n log n) time and O(n + lanes) space.

Example 1

Input:
lanes = 2, arrivals = [0,1,2,3], durations = [5,2,1,4]
Output:
[5,3,4,8]
Explanation:

Customers 0 and 1 take lanes 0 and 1 and finish at 5 and 3; customer 2 waits for lane 1 until minute 3 and finishes at 4; customer 3 starts at 4 on lane 1 and finishes at 8.

Example 2

Input:
lanes = 3arrivals = [0,0,0,0,0]durations = [1,1,1,1,1]
Output:
[1,1,1,2,2]
Explanation:

Three customers are served at minute 0 and the other two start at minute 1, so the finish times are 1, 1, 1, 2, 2.

Example 3

Input:
lanes = 5, arrivals = [10,10,20], durations = [3,3,3]
Output:
[13,13,23]
Explanation:

There are more lanes than customers, so nobody waits.

Constraints

1 ≤ lanes ≤ 105

1 ≤ n ≤ 105

0 ≤ arrivals[i] ≤ 109, non-decreasing

1 ≤ durations[i] ≤ 104

How this problem is judged

Answers
Your answer must match exactly. Numbers compare by value, so 2 and 2.0 are equal.
Time per case
Python 4,000 msC++ 1,000 msJava 2,000 msJavaScript 2,000 msTypeScript 2,000 ms

Expected complexity

Time
O(n log n)
Space
O(n + lanes)

What the author was aiming for. Your own solution is not measured against it.

Asked in an interview

Were you asked this in an interview? Say where, anonymously.

Code
Loading the editor…