1701. Average Waiting Time
MediumView on LeetCode
Time: O(n)
Space: O(1)
Problem Overview
Average Waiting Time (Medium) asks you to solve a structured algorithmic task. This is a common Array / Math pattern in coding interviews. Simulate chef's current finish time; accumulate wait time and divide by n.
A full step-by-step explanation is being added. See the study guide for pattern-based practice.
Approach
Simulate chef's current finish time; accumulate wait time and divide by n.
Related patterns: Array, Math, Simulation
1701.cs
C#
// Approach: Simulate chef's current finish time; accumulate wait time and divide by n.
// Time: O(n) Space: O(1)
public class Solution
{
public double AverageWaitingTime(int[][] customers)
{
double wait = 0, curr = 0;
foreach (int[] c in customers)
{
curr = Math.Max(curr, 1.0 * c[0]) + c[1];
wait += curr - c[0];
}
return 1.0 * wait / customers.Length;
}
}Was this solution helpful?