NeetCode #91LC-1701MediumArrays & Hashing
← Back to All Problems#91 · #1701 · Average Waiting Time(平均等待时间)
📌 Problem Statement & Constraints
There is a single chef. Given
customers[i] = [arrival_i, time_i], the chef starts cooking customer i's order no earlier than arrival_i and after finishing the previous order, and takes time_i to finish. Return the average waiting time over all customers. Constraints: 1 <= customers.length <= 10^5; customers is sorted by arrival time.💡 Core Algorithmic Approaches
- Simulate the chef's clock:
curis the time at which the chef becomes free. - For each customer, the chef starts at
max(cur, arrival), so the finish time ismax(cur, arrival) + time. - The waiting time for that customer is
finish - arrival. - Accumulate the waiting times and divide by the number of customers. Because the input is sorted by arrival, one pass is enough.
💻 Benchmark Python3 Implementation
class Solution:
def averageWaitingTime(self, customers: List[List[int]]) -> float:
cur = 0 # when the chef becomes free
total = 0
for arrival, t in customers:
cur = max(cur, arrival) + t # start no earlier than arrival
total += cur - arrival # waiting time for this customer
return total / len(customers)⚡ Complexity Deep Dive
⏱️ Time Complexity
O(n): one pass.
💾 Space Complexity
O(1): two accumulators.
⚠️ Interview Pitfalls & Follow-ups
- Using
cur += twithout themax: the chef would be modelled as starting immediately even if the customer has not arrived, which is impossible. - Summing
cur - arrivalincorrectly for the first customer: the formula handles it, sincecurstarts at 0 andmax(0, arrival) = arrival. - Returning an integer: the answer is a float; Python's
/is correct. - Re-sorting the customers: the input is already sorted by arrival, so no sort is needed.
⚡
Test muscle memory on this topic via 3-Layer Flashcards
Active Recall: Mentally formulate Prompt ➔ Math Derivation ➔ Benchmark Implementation in 3 minutes.
🧩 Related Arrays & Hashing Problems
中文速览与面试要点
平均等待时间
有一个餐厅,只有一位厨师。你有一个顾客数组 customers,其中 customers[i] = [arrivali, timei] 表示第 i 位顾客到达的时间与准备餐点所需的时间。厨师一次只能做一道菜,且必须按顾客 到达顺序 制作(顾客按到达时间非递减排列)。顾客的等待时间是「从到达时刻到餐点制作完成时刻」的时间差。返回所有顾客的平均等待时间。约束:1 ≤ customers.length ≤ 10^5;1 ≤ arrivali, timei ≤ 10^4;arrivali ≤ arrivali+1。
核心要点
- 模拟厨师的时间线(标准解) :维护当前时刻 cur 。对每位顾客,厨师开始制作的时间是 max(cur, arrival) 。
- 更新当前时刻 : cur = max(cur, arrival) + time ,即餐点完成的时刻。
- 累加等待时间 :该顾客的等待时间为 cur - arrival 。
- max(cur, arrival) 的含义 :若厨师空闲(cur < arrival),则需等顾客到达;若厨师还在忙(cur ≥ arrival),则顾客需排队等待。
常见陷阱
- 忘记 max(cur, arrival) 而直接 cur += time :会忽略「顾客还没到、厨师需要空等」的情况,导致等待时间被严重低估。
- 等待时间算成 time :等待时间包含排队时间,因此是 cur - arrival 而非单纯的制作时长。
- 返回整数除法 :Python 3 的 / 自动返回 float,但若在 C++/Java 中写成整数除法会丢失精度。
- 误以为可以重排顾客顺序 :题目要求必须按到达顺序制作,不能为了降低平均等待时间而重排(那会变成调度优化问题)。
参考实现
class Solution:
def averageWaitingTime(self, customers: list[list[int]]) -> float:
cur = 0 # 厨师完成上一道菜的时刻
total = 0
for arrival, time in customers:
cur = max(cur, arrival) + time # 开始时间 = max(厨师空闲, 顾客到达)
total += cur - arrival # 等待时间 = 完成时刻 - 到达时刻
return total / len(customers)