Skip to main content
Back to problems
#1701
Medium Algorithms

Average waiting time

Array Simulation
73.1% acceptance
Feb 25, 2026
1269
101
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 ith customer. The arrival times are sorted in non-decreasing order. timei is the time needed to prepare the order of the ith 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.

Solution

Rust
Time O(n)
Space O(1)
LeetCode
solution.rs
impl Solution {
  pub fn average_waiting_time(customers: Vec<Vec<i32>>) -> f64 {
    let mut current_time: i64 = 0;
    let mut total_wait: i64 = 0;
    for c in &customers {
      let arrival = c[0] as i64;
      let time = c[1] as i64;
      if current_time < arrival {
        current_time = arrival;
      }
      current_time += time;
      total_wait += current_time - arrival;
    }
    total_wait as f64 / customers.len() as f64
  }
}