DDSA
Advertisement

1701. Average Waiting Time

Time: O(n)
Space: O(1)

Approach

Simulate chef's current finish time; accumulate wait time and divide by n.

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;
    }
}
Advertisement
Was this solution helpful?