DDSA Solutions

1701. Average Waiting Time

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

Approach

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

Key Techniques

Array

Array problems involve manipulating elements stored in a contiguous block of memory. Key techniques include two-pointer traversal, prefix sums, sliding windows, and in-place partitioning. In C#, arrays are zero-indexed and fixed in size — use List<T> when you need dynamic resizing.

Math

Math problems test number theory, combinatorics, and modular arithmetic. Common tools: GCD/LCM (Euclidean algorithm), prime sieve, modular inverse (Fermat's little theorem), digit manipulation, and bit tricks. Overflow is a key concern in C# — use long when products may exceed 2³¹.

Simulation

Simulation problems require implementing the described process step by step. Focus on correctly handling edge cases and state transitions. Common in geometry, game problems, and string manipulation. Optimize only if the naive simulation exceeds the time limit.

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?