返回介绍

solution / 1700-1799 / 1701.Average Waiting Time / README_EN

发布于 2024-06-17 01:03:15 字数 5836 浏览 0 评论 0 收藏 0

1701. Average Waiting Time

中文文档

Description

There is a restaurant with a single chef. You are given an array customers, where customers[i] = [arrivali, timei]:

  • arrivali is the arrival time of the ith customer. The arrival times are sorted in non-decreasing order.
  • timei is the time needed to prepare the order of the ith customer.

When a customer arrives, he gives the chef his order, and the chef starts preparing it once he is idle. The customer waits till the chef finishes preparing his order. The chef does not prepare food for more than one customer at a time. The chef prepares food for customers in the order they were given in the input.

Return _the average waiting time of all customers_. Solutions within 10-5 from the actual answer are considered accepted.

 

Example 1:

Input: customers = [[1,2],[2,5],[4,3]]
Output: 5.00000
Explanation:
1) The first customer arrives at time 1, the chef takes his order and starts preparing it immediately at time 1, and finishes at time 3, so the waiting time of the first customer is 3 - 1 = 2.
2) The second customer arrives at time 2, the chef takes his order and starts preparing it at time 3, and finishes at time 8, so the waiting time of the second customer is 8 - 2 = 6.
3) The third customer arrives at time 4, the chef takes his order and starts preparing it at time 8, and finishes at time 11, so the waiting time of the third customer is 11 - 4 = 7.
So the average waiting time = (2 + 6 + 7) / 3 = 5.

Example 2:

Input: customers = [[5,2],[5,4],[10,3],[20,1]]
Output: 3.25000
Explanation:
1) The first customer arrives at time 5, the chef takes his order and starts preparing it immediately at time 5, and finishes at time 7, so the waiting time of the first customer is 7 - 5 = 2.
2) The second customer arrives at time 5, the chef takes his order and starts preparing it at time 7, and finishes at time 11, so the waiting time of the second customer is 11 - 5 = 6.
3) The third customer arrives at time 10, the chef takes his order and starts preparing it at time 11, and finishes at time 14, so the waiting time of the third customer is 14 - 10 = 4.
4) The fourth customer arrives at time 20, the chef takes his order and starts preparing it immediately at time 20, and finishes at time 21, so the waiting time of the fourth customer is 21 - 20 = 1.
So the average waiting time = (2 + 6 + 4 + 1) / 4 = 3.25.

 

Constraints:

  • 1 <= customers.length <= 105
  • 1 <= arrivali, timei <= 104
  • arrival<= arrivali+1

Solutions

Solution 1: Simulation

We use a variable tot to record the total waiting time of the customers, and a variable t to record the time when each customer's order is completed. The initial values of both are $0$.

We traverse the customer array customers. For each customer:

If the current time t is less than or equal to the customer's arrival time customers[i][0], it means that the chef is not cooking, so the chef can start cooking immediately. The time to complete this dish is $t = customers[i][0] + customers[i][1]$, and the customer's waiting time is customers[i][1].

Otherwise, it means that the chef is cooking, so the customer needs to wait for the chef to finish the previous dishes before starting to cook their own dishes. The time to complete this dish is $t = t + customers[i][1]$, and the customer's waiting time is $t - customers[i][0]$.

The time complexity is $O(n)$, where $n$ is the length of the customer array customers. The space complexity is $O(1)$.

class Solution:
  def averageWaitingTime(self, customers: List[List[int]]) -> float:
    tot = t = 0
    for a, b in customers:
      t = max(t, a) + b
      tot += t - a
    return tot / len(customers)
class Solution {
  public double averageWaitingTime(int[][] customers) {
    double tot = 0;
    int t = 0;
    for (var e : customers) {
      int a = e[0], b = e[1];
      t = Math.max(t, a) + b;
      tot += t - a;
    }
    return tot / customers.length;
  }
}
class Solution {
public:
  double averageWaitingTime(vector<vector<int>>& customers) {
    double tot = 0;
    int t = 0;
    for (auto& e : customers) {
      int a = e[0], b = e[1];
      t = max(t, a) + b;
      tot += t - a;
    }
    return tot / customers.size();
  }
};
func averageWaitingTime(customers [][]int) float64 {
  tot, t := 0, 0
  for _, e := range customers {
    a, b := e[0], e[1]
    t = max(t, a) + b
    tot += t - a
  }
  return float64(tot) / float64(len(customers))
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文