Average Waiting Time

A restaurant has one chef. You are given `customers`, where `customers[i] = [arrival_i, time_i]`: customer i arrives at time `arrival_i` and their order takes `time_i` to prepare. Arrival times are sorted in non-decreasing order. The chef cooks one order at a time, in the order customers arrive, and starts an order as soon as both the chef is free and the customer has arrived. A customer waits from their arrival until their order is finished. Return the average waiting time of all customers. Answers within $10^{-5}$ of the true value are accepted.

30:00

Average Waiting Time
medium
Free written solution
Topics
Companies

A restaurant has one chef. You are given customers, where customers[i] = [arrival_i, time_i]: customer i arrives at time arrival_i and their order takes time_i to prepare. Arrival times are sorted in non-decreasing order.

The chef cooks one order at a time, in the order customers arrive, and starts an order as soon as both the chef is free and the customer has arrived. A customer waits from their arrival until their order is finished. Return the average waiting time of all customers. Answers within 10−510^{-5} of the true value are accepted.

Example 1:
Input: {"customers":[[1,2],[2,5],[4,3]]}
Output: 5.0
Constraints:
  • 1≤customers.length≤1051 \leq \text{customers.length} \leq 10^5

  • 1≤arrivali,timei≤1041 \leq \text{arrival}_i, \text{time}_i \leq 10^4

  • arrivali≤arrivali+1\text{arrival}_i \leq \text{arrival}_{i+1}

Test Cases
Input (Stdin)
Expected Output (Stdout)
Execution Result

Run code to see results.

Input
arr ={"customers":[[1,2],[2,5],[4,3]]}

The chef starts free at time 0 and no one has waited yet. 3 customers will arrive in order.

chef free at

0

total wait

0

served

0/3

avg so far

0.0

kitchen timeline

1234chefc0c1c2
● arrives■ queued■ cooking+w = wait (finish - arrival)
Variables
VariableValue
clock0
total0
DepthFunction Call
Stack empty
0/7