Advertisement

Average Waiting Time - LeetCode 1701 Solution

Average Waiting Time - Complete Solution Guide

Average Waiting Time is LeetCode problem 1701, a Medium level challenge. This complete guide provides step-by-step explanations, multiple solution approaches, and optimized code in python3, java, cpp, c.

Problem Statement

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 mo

Detailed Explanation

The problem asks us to calculate the average waiting time for customers at a restaurant with a single chef. We are given a list of customers, where each customer is represented by their arrival time and the time it takes to prepare their order. The chef serves customers in the order they arrive, and the chef can only work on one order at a time. The waiting time for a customer is the time they spend waiting between arriving and receiving their completed order. The input `customers` is sorted by arrival time in non-decreasing order. We need to compute the average of all customers' waiting times.

Solution Approach

The solution uses a simulation approach. We iterate through the `customers` array, maintaining a variable `current_time` that represents the time the chef is available to start a new order. For each customer, we update `current_time` to be the maximum of the customer's arrival time and the chef's current available time. We then add the customer's preparation time to `current_time`. The waiting time for that customer is calculated by subtracting the arrival time from `current_time`. We sum up the waiting times for all customers and then divide by the number of customers to get the average waiting time.

Step-by-Step Algorithm

  1. Step 1: Initialize `total_wait_time` to 0 and `current_time` to 0.
  2. Step 2: Iterate through the `customers` array.
  3. Step 3: For each customer, extract the arrival time (`arrival`) and preparation time (`prep_time`).
  4. Step 4: Update `current_time` to `max(arrival, current_time) + prep_time`. This represents the time the chef finishes preparing the customer's order.
  5. Step 5: Calculate the waiting time for the current customer as `current_time - arrival`.
  6. Step 6: Add the waiting time to `total_wait_time`.
  7. Step 7: After iterating through all customers, calculate the average waiting time by dividing `total_wait_time` by the number of customers.
  8. Step 8: Return the average waiting time.

Key Insights

  • Insight 1: The chef starts working on a customer's order as soon as the chef is idle or the customer arrives, whichever is later.
  • Insight 2: To calculate the waiting time for each customer, we need to keep track of the current time the chef is available.
  • Insight 3: Since the customer arrival times are sorted, we can iterate through the customers array in order to simulate the chef's workflow and maintain the current time.

Complexity Analysis

Time Complexity: O(n)

Space Complexity: O(1)

Topics

This problem involves: Array, Simulation.

Companies

Asked at: DE Shaw, Instacart.