1701. Average Waiting Time - Explanation

Problem Link

Description

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

  • arrival[i] is the arrival time of the i-th customer. The arrival times are sorted in non-decreasing order.

  • time[i] 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 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.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.

Example 2:

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 <= arrivali, timei <= 10^4
  • arrival[i] <= arrival[i+1]


Topics

Company Tags

Please upgrade to NeetCode Pro to view company tags.



Prerequisites

Before attempting this problem, you should be comfortable with:

  • Simulation - Processing events in order and tracking state over time
  • Linear Array Traversal - Iterating through customers in arrival order
  • Basic Arithmetic - Calculating wait times using max operations and running totals

1. Simulation - I

Intuition

We simulate the chef serving customers one by one. The chef can only start a new order when they finish the previous one. If a customer arrives while the chef is busy, they wait. The waiting time for each customer is the time from arrival until their order is complete. We track the current time and accumulate total waiting time.

Algorithm

  1. Initialize t (current time) and total (total waiting time) to 0.
  2. For each customer with arrival time and order duration:
    • If the chef is still busy (t > arrival), add the extra wait to total.
    • Otherwise, update t to the arrival time.
    • Add the order time to both total and t.
  3. Return total divided by the number of customers.
class Solution:
    def averageWaitingTime(self, customers: List[List[int]]) -> float:
        t = 0
        total = 0

        for arrival, order in customers:
            if t > arrival:
                total += t - arrival
            else:
                t = arrival
            total += order
            t += order

        return total / len(customers)

Time & Space Complexity

  • Time complexity: O(n)O(n)
  • Space complexity: O(1)O(1)

2. Simulation - II

Intuition

This is a more concise version of the simulation. The key observation is that the chef starts cooking at whichever is later: their current finish time or the customer's arrival. The finish time becomes max(t, arrival) + order. The customer's waiting time is simply finish_time - arrival. This formula captures both cases (chef idle or busy) in one expression.

Algorithm

  1. Initialize t (finish time) and total to 0.
  2. For each customer:
    • Update t to max(t, arrival) + order (when the order finishes).
    • Add t - arrival to total (customer's wait time).
  3. Return total divided by the number of customers.
class Solution:
    def averageWaitingTime(self, customers: List[List[int]]) -> float:
        t = total = 0
        for arrival, order in customers:
            t = max(t, arrival) + order
            total += t - arrival
        return total / len(customers)

Time & Space Complexity

  • Time complexity: O(n)O(n)
  • Space complexity: O(1)O(1)

Common Pitfalls

Integer Overflow

With large arrival times and order durations, the sum of waiting times can exceed 32-bit integer limits. Use 64-bit integers for accumulating totals.

// Wrong: int can overflow
int total = 0;
// Right: use long for accumulation
long total = 0;

Confusing Waiting Time with Service Time

Waiting time is from arrival until the order is complete (includes cooking time), not just the time spent waiting before cooking starts.

# Wrong: only counting idle wait time
wait = max(0, t - arrival)
# Right: total time from arrival to completion
wait = (max(t, arrival) + order) - arrival

Forgetting to Update Current Time

When a customer arrives after the chef is idle, the current time must jump forward to the arrival time, not stay at the previous finish time.

# Wrong: always adding order time to current t
t += order
# Right: chef starts at max(t, arrival)
t = max(t, arrival) + order