> ## Documentation Index
> Fetch the complete documentation index at: https://leetcode-py.wisl.dev/llms.txt
> Use this file to discover all available pages before exploring further.

> ## Agent Instructions
> leetcode-py is a Python LeetCode practice environment generator with one CLI: lcpy. It is not a service or platform.
> Each problem is a directory under leetcode/ with README.md, solution.py, test_solution.py, helpers.py, and playground.ipynb. lcpy gen creates them from JSON templates bundled with the package.
> Examples are backed by tests; copy them verbatim.

# Average Waiting Time Python Solution

> Tested Python solution for LeetCode 1701 with 14 pytest cases. Generate a practice environment with lcpy.

LeetCode 1701, [Medium](/catalog/medium). Topics: [Array](/catalog/topics/array), [Simulation](/catalog/topics/simulation). [View on LeetCode](https://leetcode.com/problems/average-waiting-time/description/).

Generate this problem as a practice environment: tested reference solution, 14 [parametrized pytest cases](/practice/testing), and a playground notebook:

```bash theme={"theme":{"light":"github-light","dark":"github-dark"}}
lcpy gen -n 1701   # by problem number
lcpy gen -s average_waiting_time   # by problem name
```

## Problem

There is a restaurant with a single chef. You are given an array `customers`, where `customers[i] = [arrival<sub>i</sub>, time<sub>i</sub>]`:

* `arrival<sub>i</sub>` is the arrival time of the `i<sup>th</sup>` customer. The arrival times are sorted in non-decreasing order.
* `time<sub>i</sub>` is the time needed to prepare the order of the `i<sup>th</sup>` customer.

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.

### Examples

```
Input: customers = [[1,2],[2,5],[4,3]]
Output: 5.00000
Explanation:
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.
```

```
Input: customers = [[5,2],[5,4],[10,3],[20,1]]
Output: 3.25000
Explanation:
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\<sub>i\</sub>, time\<sub>i\</sub> \<= 10^4
* arrival\<sub>i\</sub> \<= arrival\<sub>i+1\</sub>

## Solution

Reference implementation from [solution.py on GitHub](https://github.com/wislertt/leetcode-py/blob/main/leetcode/average_waiting_time/solution.py), full suite in [test\_solution.py](https://github.com/wislertt/leetcode-py/blob/main/leetcode/average_waiting_time/test_solution.py):

```python theme={"theme":{"light":"github-light","dark":"github-dark"}}
class Solution:
    # Time: O(n)
    # Space: O(1)
    def average_waiting_time(self, customers: list[list[int]]) -> float:
        now = 0
        total_wait = 0
        for arrival, time in customers:
            now = max(now, arrival) + time
            total_wait += now - arrival
        return total_wait / len(customers)
```

## Complexity

| Time | Space |
| - | - |
| O(n) | O(1) |

## Tags

[NeetCode All](/catalog/neetcode).


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.