Mid/SeniorArraySimulation

Average Waiting Time

There is a restaurant with a single chef. You are given an array customers, where customers[i] = [arrivali, timei]:

  • arrivali is the arrival time of the i^th customer. The arrival times are sorted in non-decreasing order.
  • timei is the time needed to prepare the order of the i^th customer.

When a customer arrives, he gives the chef his order, and the chef starts preparing it once he is idle. The customer waits until 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
Inputcustomers = [[1,2],[2,5],[4,3]]
Output5
The waiting times are 2, 6, and 7, so the average waiting time is (2 + 6 + 7) / 3 = 5.
Example 2
Inputcustomers = [[5,2],[5,4],[10,3],[20,1]]
Output3.25
The waiting times are 2, 6, 4, and 1, so the average waiting time is (2 + 6 + 4 + 1) / 4 = 3.25.

Constraints

  • 1 <= customers.length <= 10^5
  • 1 <= arrivali, timei <= 10^4
  • arrivali <= arrivali+1

Asked at 6 companies

</>

Your Solution

(Ctrl/Cmd + Enter)

Switching Language

Loading template...

Loading...

Sign in to save your progress

AI code evaluation

Get a correctness verdict, missed edge cases, and complexity analysis of your solution.

Sign in to evaluate