짬뽕얼큰하게의 맨땅에 헤딩 :: Average Waiting Time

summary

  • Calculate the average waiting time of customers in a restaurant.

approach

  • This solution uses a greedy approach to calculate the average waiting time of customers
  • It iterates over the customers and calculates the waiting time for each customer based on the time the chef is idle
  • The total waiting time is then divided by the number of customers to get the average waiting time.

complexity

  • O(n)

explain

  • The solution works by iterating over the customers and calculating the waiting time for each customer
  • If the customer arrives after the chef is idle, the waiting time is the time the chef has been idle plus the preparation time
  • If the customer arrives before the chef is idle, the waiting time is the time the chef has been idle plus the preparation time minus the time the customer arrived
  • The total waiting time is then divided by the number of customers to get the average waiting time.

Solution Code:


class Solution {
public:
    double averageWaitingTime(vector>& customers) {
        long long totalWait = 0;
        long long time = 0;
        for(int i = 0 ; i < customers.size(); i++){
            int arrival = customers[i][0];
            int prepareTime = customers[i][1];

            if (arrival > time){
                time = arrival + prepareTime;
                totalWait += prepareTime;
            } else {
                totalWait += time - arrival + prepareTime;
                time += prepareTime;
            }
        }
        return (double)totalWait / customers.size();
    }
};

'알고리즘' 카테고리의 다른 글

Reverse Substrings Between Each Pair of Parentheses  (0) 2025.02.02
Crawler Log Folder  (0) 2025.02.02
Find the Winner of the Circular Game  (0) 2025.02.02
Water Bottles  (0) 2025.02.02
Pass the Pillow  (0) 2025.02.02
블로그 이미지

짬뽕얼큰하게

,