Average Waiting Time — LeetCode 1701 Python Solution
MediumArraySimulation
- Problem
- #1701
- Reading time
- 2 min
- Source
- leetcode.com
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
| Measure | Complexity |
|---|---|
| Time | O(n), where n is the length of the customer array `customers` |
| Space | O(1) auxiliary |
Related problems
LeetCode 495Teemo AttackingEasyLeetCode 985Sum of Even Numbers After QueriesMediumLeetCode 1389Create Target Array in the Given OrderEasyLeetCode 1409Queries on a Permutation With KeyMediumLeetCode 1503Last Moment Before All Ants Fall Out of a PlankMediumLeetCode 1535Find the Winner of an Array GameMedium
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.