Average Waiting Time

MediumArraySimulation

Solution

export function averageWaitingTime(customers: number[][]): number {
  const n = customers.length;
  let currentTime = 0;
  let totalWaitingTime = 0;
  for (const [arrival, time] of customers) {
    currentTime = Math.max(currentTime, arrival) + time;
    totalWaitingTime += currentTime - arrival;
  }
  return totalWaitingTime / n;
}

Complexity

  • Time: O(N)
  • Space: O(1)