1678. Impatient Customers

Easy1.5 points estimatedQueueSimulationArray

A single clerk serves a first-come, first-served line. Customer i arrives at minute arrive[i] (the arrival times never decrease) and needs serve[i] minutes at the counter. The clerk is free at minute 0 and handles customers in arrival order. Customer i would begin at the later of the minute the clerk becomes free and arrive[i]. If that wait is greater than patience[i] the customer gives up and leaves, and the clerk's schedule does not change; otherwise the clerk is busy for serve[i] minutes starting then.

Return how many customers are served. Process the line in a single pass with O(n) time and O(1) extra space, using 64-bit arithmetic for the times. Constraints: 1 <= n <= 1000, 0 <= arrive[i] <= 10^9 and non-decreasing, 1 <= serve[i] <= 10^4, 0 <= patience[i] <= 10^9.

Example 1

Input:
arrive = [0,1,2], serve = [5,5,5], patience = [0,3,3]
Output:
2
Explanation:

Customer 0 is served until minute 5, customer 1 would wait 4 > 3 and leaves, customer 2 waits exactly 3 and is served.

Example 2

Input:
arrive = [0,0,0], serve = [2,2,2], patience = [0,0,0]
Output:
1
Explanation:

Only the first customer starts without waiting; the other two would wait 2 minutes and leave.

Example 3

Input:
arrive = [10,20], serve = [3,3], patience = [0,0]
Output:
2
Explanation:

The clerk is idle when each customer arrives, so both are served.

Constraints

1 ≤ n ≤ 1000; 0 ≤ arrive[i] ≤ 109, non-decreasing; 1 ≤ serve[i] ≤ 104; 0 ≤ patience[i] ≤ 109; arrive, serve and patience have equal length

How this problem is judged

Answers
Your answer must match exactly. Numbers compare by value, so 2 and 2.0 are equal.

Expected complexity

Time
O(n)
Space
O(1)

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…