Show problems as
Classic problem #1701MediumArraySimulation

Average Waiting Time

Real-world version: Average Wait at a Single Press
Tracked in this browser only
Read Average Waiting Time on LeetCode(opens in a new tab)

The statement here is our own: the same inputs, outputs and technique.

One press serves jobs in the order they arrive. customers[i] is a pair [arrival, minutes]: the minute the i-th job is handed in, and how long the press takes on it once started. Arrivals are given in non-decreasing order.

The press works on one job at a time, start to finish, and takes the next waiting job the moment it is free. If nothing is waiting, it idles until the next job arrives. A job's wait is the span from when it was handed in to when the press finishes it.

Return the average wait across all the jobs. An answer within 1e-5 of the true value is accepted.

Examples

Example 1

Input
customers = [[3, 7], [4, 2], [12, 5]]
Output
6.666666666666667

The first job runs from minute 3 to 10, waiting 7. The second was handed in at 4 but starts at 10 and ends at 12, waiting 8. The press then idles until minute 12 and finishes the third at 17, waiting 5. The three waits average to 20 divided by 3.

Example 2

Input
customers = [[1, 10], [2, 1], [3, 1], [4, 1]]
Output
10.0

One long job at the front holds up the three short ones behind it, which finish at minutes 12, 13 and 14.

Example 3

Input
customers = [[1, 1], [100, 1]]
Output
1.0

The press is long since free when the second job arrives, so neither job waits for anything but its own minute on the press.

Constraints

  • 1 <= customers.length <= 10^5
  • customers[i].length == 2
  • 1 <= customers[i][0] <= 10^4
  • 1 <= customers[i][1] <= 10^4
  • customers[i][0] <= customers[i + 1][0]

The signature

The editor starts with this. It matches the parent problem's shape, so a solution that works here works on a standard judge unchanged.

Python
def average_waiting_time(customers: list[list[int]]) -> float:
Java
public double averageWaitingTime(int[][] customers)