LeetCode 1701: Average Waiting Time Solution

Master LeetCode problem 1701 (Average Waiting Time), a medium challenge, with our optimized solutions in Java, C++, and Python. Explore detailed explanations, test your code in our interactive editor, and prepare for coding interviews.

1701. Average Waiting Time

Problem Explanation

Explanation

To solve this problem, we iterate through the customers array and maintain the current time and the finish time of the chef's current order. We calculate the waiting time for each customer and update the current time accordingly. Finally, we calculate the average waiting time based on the total waiting time and the number of customers.

Algorithm:

  1. Initialize variables: currentTime = 0, totalWaitTime = 0.
  2. Iterate through each customer in the customers array.
  3. Calculate the finish time for the current customer as max(currentTime, customer[0]) + customer[1].
  4. Calculate the waiting time for the current customer as finish time - customer[0].
  5. Update totalWaitTime by adding the waiting time.
  6. Update currentTime to the finish time.
  7. Calculate the average waiting time as totalWaitTime divided by the number of customers.

Time Complexity: O(N), where N is the number of customers. Space Complexity: O(1).

Solution Code

class Solution {
    public double averageWaitingTime(int[][] customers) {
        int n = customers.length;
        int currentTime = 0;
        int totalWaitTime = 0;
        
        for (int i = 0; i < n; i++) {
            int finishTime = Math.max(currentTime, customers[i][0]) + customers[i][1];
            int waitingTime = finishTime - customers[i][0];
            totalWaitTime += waitingTime;
            currentTime = finishTime;
        }
        
        return (double) totalWaitTime / n;
    }
}

Try It Yourself

Loading code editor...

Related LeetCode Problems

Frequently Asked Questions

How to solve LeetCode 1701 (Average Waiting Time)?

This page provides optimized solutions for LeetCode problem 1701 (Average Waiting Time) in Java, C++, and Python, along with a detailed explanation and an interactive code editor to test your code.

What is the time complexity of LeetCode 1701 (Average Waiting Time)?

The time complexity for LeetCode 1701 (Average Waiting Time) varies by solution. Check the detailed explanation section for specific complexities in Java, C++, and Python implementations.

Can I run code for LeetCode 1701 on DevExCode?

Yes, DevExCode provides an interactive code editor where you can write, test, and run your code for LeetCode 1701 in Java, C++, or Python.

Back to LeetCode Solutions