aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJack Sangdahl <[email protected]>2024-07-08 19:58:12 -0600
committerJack Sangdahl <[email protected]>2024-07-08 19:58:12 -0600
commitd4a293cacfd8085e968e4341b9b779a294ff938b (patch)
tree62bf87cc142c85ced1cf4125dd36c3696d29e5c0
parent9fe7844d8621cd8cc30b5269e9236e3ac51a1542 (diff)
downloadleetcode-d4a293cacfd8085e968e4341b9b779a294ff938b.tar.gz
leetcode-d4a293cacfd8085e968e4341b9b779a294ff938b.zip
1701. average waiting time
-rw-r--r--average_waiting_time.c34
-rw-r--r--average_waiting_time.py31
2 files changed, 65 insertions, 0 deletions
diff --git a/average_waiting_time.c b/average_waiting_time.c
new file mode 100644
index 0000000..fbd045e
--- /dev/null
+++ b/average_waiting_time.c
@@ -0,0 +1,34 @@
+// 1701. Average Waiting Time
+#include "leetcode.h"
+
+/*
+ * there is a restaurant with a single chef. you are given an array 'customers'
+ * where 'customers[i] = [arrivali, timei]'. where 'arrivali' is the arrival
+ * time of the i'th customer. the arrival times are sorted in non decreasing
+ * order, and 'timei' is the time needed to prepare the order of the i'th
+ * customer. when a customer arrives, he gives the chef his order and the chef
+ * prepares it. return the average waiting time of all customers. solutions
+ * within 10^-5 from actually answer are considered accepted
+ */
+
+double averageWaitingTime(int **customers, int customersSize,
+ int *customersColSize) {
+ double wait = 0;
+ int curr = 0;
+ for (int i = 0; i < customersSize; i++) {
+ curr = fmax(curr, customers[i][0]);
+ wait += curr + customers[i][1] - customers[i][0];
+ curr += customers[i][1];
+ }
+ return wait / customersSize;
+}
+
+int main() {
+ int c1[3][2] = {{1, 2}, {2, 5}, {4, 3}},
+ c2[4][2] = {{5, 2}, {5, 4}, {10, 3}, {20, 1}};
+ int ccs1[] = {2, 2, 2}, ccs2[] = {2, 2, 2, 2};
+ printf("%f\n", averageWaitingTime((int **)c1, ARRAY_SIZE(c1),
+ ccs1)); // expect: 5.0000
+ printf("%f\n", averageWaitingTime((int **)c2, ARRAY_SIZE(c2),
+ ccs2)); // expect: 3.25000
+}
diff --git a/average_waiting_time.py b/average_waiting_time.py
new file mode 100644
index 0000000..b6173cf
--- /dev/null
+++ b/average_waiting_time.py
@@ -0,0 +1,31 @@
+# 1701. Average Waiting Time
+
+"""
+there is a restaurant with a single chef. you are given an array 'customers'
+where 'customers[i] = [arrivali, timei]'. where 'arrivali' is the arrival
+time of the i'th customer. the arrival times are sorted in non decreasing
+order, and 'timei' is the time needed to prepare the order of the i'th
+customer. when a customer arrives, he gives the chef his order and the chef
+prepares it. return the average waiting time of all customers. solutions
+within 10^-5 from actually answer are considered accepted
+"""
+
+
+class Solution(object):
+ def averageWaitingTime(self, customers):
+ """
+ :type customers: List[List[int]]
+ :rtype: float
+ """
+ wait, curr = 0, 0
+ for c in customers:
+ curr = max(curr, c[0]) + c[1]
+ wait += curr - c[0]
+ avg = wait / len(customers)
+ return avg
+
+
+if __name__ == "__main__":
+ obj = Solution()
+ print(obj.averageWaitingTime(customers=[[1, 2], [2, 5], [4, 3]]))
+ print(obj.averageWaitingTime(customers=[[5, 2], [5, 4], [10, 3], [20, 1]]))