There is a restaurant with a single chef. You are given an array `customers`

, where `customers[i] = [arrival`

_{i}, time_{i}]:

`arrival`

is the arrival time of the_{i}`i`

customer. The arrival times are sorted in^{th}**non-decreasing**order.`time`

is the time needed to prepare the order of the_{i}`i`

customer.^{th}

When a customer arrives, he gives the chef his order, and the chef starts preparing it once he is idle. The customer waits till the chef finishes preparing his order. The chef does not prepare food for more than one customer at a time. The chef prepares food for customers **in the order they were given in the input**.

Return *the average waiting time of all customers*. Solutions within

`10`^{-5}

from the actual answer are considered accepted.**Example 1:**

Input:customers = [[1,2],[2,5],[4,3]]Output:5.00000Explanation:1) The first customer arrives at time 1, the chef takes his order and starts preparing it immediately at time 1, and finishes at time 3, so the waiting time of the first customer is 3 - 1 = 2. 2) The second customer arrives at time 2, the chef takes his order and starts preparing it at time 3, and finishes at time 8, so the waiting time of the second customer is 8 - 2 = 6. 3) The third customer arrives at time 4, the chef takes his order and starts preparing it at time 8, and finishes at time 11, so the waiting time of the third customer is 11 - 4 = 7. So the average waiting time = (2 + 6 + 7) / 3 = 5.

**Example 2:**

Input:customers = [[5,2],[5,4],[10,3],[20,1]]Output:3.25000Explanation:1) The first customer arrives at time 5, the chef takes his order and starts preparing it immediately at time 5, and finishes at time 7, so the waiting time of the first customer is 7 - 5 = 2. 2) The second customer arrives at time 5, the chef takes his order and starts preparing it at time 7, and finishes at time 11, so the waiting time of the second customer is 11 - 5 = 6. 3) The third customer arrives at time 10, the chef takes his order and starts preparing it at time 11, and finishes at time 14, so the waiting time of the third customer is 14 - 10 = 4. 4) The fourth customer arrives at time 20, the chef takes his order and starts preparing it immediately at time 20, and finishes at time 21, so the waiting time of the fourth customer is 21 - 20 = 1. So the average waiting time = (2 + 6 + 4 + 1) / 4 = 3.25.

**Constraints:**

`1 <= customers.length <= 10`

^{5}`1 <= arrival`

_{i}, time_{i}<= 10^{4}`arrival`

_{i }<= arrival_{i+1}

**Solution: Simulation**

When a customer arrives, if the arrival time is greater than current, then advance the clock to arrival time. Advance the clock by cooking time. Waiting time = current time – arrival time.

Time complexity: O(n)

Space complexity: O(1)

## C++

1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
// Author: Huahua class Solution { public: double averageWaitingTime(vector<vector<int>>& customers) { int t = 0; double w = 0; for (const auto& c : customers) { if (c[0] > t) t = c[0]; t += c[1]; w += t - c[0]; } return w / customers.size(); } }; |

## Python3

1 2 3 4 5 6 7 8 9 |
# Author: Huahua class Solution: def averageWaitingTime(self, customers: List[List[int]]) -> float: cur = 0 waiting = 0 for arrival, time in customers: cur = max(cur, arrival) + time waiting += cur - arrival return waiting / len(customers) |

请尊重作者的劳动成果，转载请注明出处！花花保留对文章／视频的所有权利。

如果您喜欢这篇文章／视频，欢迎您捐赠花花。

If you like my articles / videos, donations are welcome.

## Be First to Comment