Average Waiting Time — LeetCode 1701 Python Solution

MediumArraySimulation
Problem
#1701
Reading time
2 min

The problem

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.

Example

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.

Python solution

Python
class Solution:
    def averageWaitingTime(self, customers: List[List[int]]) -> float:
        tot = t = 0
        for a, b in customers:
            t = max(t, a) + b
            tot += t - a
        return tot / len(customers)

Complexity

MeasureComplexity
TimeO(n), where n is the length of the customer array `customers`
SpaceO(1) auxiliary

Related problems

Frequently asked questions

How hard is LeetCode 1701. Average Waiting Time?
LeetCode 1701. Average Waiting Time is rated Medium on LeetCode.
What is the time complexity of LeetCode 1701. Average Waiting Time?
The Python solution on this page runs in O(n), where n is the length of the customer array `customers`.
What is the space complexity of LeetCode 1701. Average Waiting Time?
The Python solution on this page uses O(1) auxiliary space.
What topics does LeetCode 1701. Average Waiting Time cover?
LeetCode 1701. Average Waiting Time is tagged Array and Simulation on LeetCode.

Stuck on problems like this in a live interview?

Stealth Interview is a desktop app for macOS and Windows. It reads the problem off your screen and returns a working solution with a step-by-step explanation and its time and space complexity — invisible to screen sharing.

Get Stealth Interview