Skip to content

Commit 1ad8574

Browse files
committed
feat: add solutions to lc problem: No.1701
No.1701.Average Waiting Time
1 parent f2be975 commit 1ad8574

File tree

6 files changed

+154
-34
lines changed

6 files changed

+154
-34
lines changed

solution/1700-1799/1701.Average Waiting Time/README.md

Lines changed: 62 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,17 @@
5858

5959
<!-- 这里可写通用的实现逻辑 -->
6060

61-
记 totalWaitingTime 表示总等待时间,f 表示当次做菜完成时间。
61+
**方法一:模拟**
62+
63+
我们用变量 `tot` 记录顾客的总等待时间,用变量 `t` 记录做完每个顾客的订单的时间,初始值均为 $0$。
64+
65+
遍历顾客数组 `customers`,对于每个顾客:
66+
67+
如果当前时间 `t` 小于等于顾客的到达时间 `customers[i][0]`,说明厨师没有在做菜,那么厨师可以立即开始做菜,做完这道菜的时间为 `t = customers[i][0] + customers[i][1]`,顾客的等待时间为 `customers[i][1]`
68+
69+
否则,说明厨师正在做菜,那么顾客需要等待厨师做完此前的菜,才能开始做自己的菜,做完这道菜的时间为 `t = t + customers[i][1]`,顾客的等待时间为 `t - customers[i][0]`
70+
71+
时间复杂度 $O(n)$,空间复杂度 $O(1)$。其中 $n$ 为顾客数组 `customers` 的长度。
6272

6373
<!-- tabs:start -->
6474

@@ -69,11 +79,11 @@
6979
```python
7080
class Solution:
7181
def averageWaitingTime(self, customers: List[List[int]]) -> float:
72-
f = total_waiting_time = 0
73-
for arrival, time in customers:
74-
f = max(arrival, f) + time
75-
total_waiting_time += f - arrival
76-
return total_waiting_time / len(customers)
82+
tot = t = 0
83+
for a, b in customers:
84+
t = max(t, a) + b
85+
tot += t - a
86+
return tot / len(customers)
7787
```
7888

7989
### **Java**
@@ -83,14 +93,54 @@ class Solution:
8393
```java
8494
class Solution {
8595
public double averageWaitingTime(int[][] customers) {
86-
int f = 0;
87-
double totalWaitingTime = 0;
88-
for (int[] customer : customers) {
89-
f = Math.max(f, customer[0]) + customer[1];
90-
totalWaitingTime += (f - customer[0]);
96+
double tot = 0;
97+
int t = 0;
98+
for (var e : customers) {
99+
int a = e[0], b = e[1];
100+
t = Math.max(t, a) + b;
101+
tot += t - a;
102+
}
103+
return tot / customers.length;
104+
}
105+
}
106+
```
107+
108+
### **C++**
109+
110+
```cpp
111+
class Solution {
112+
public:
113+
double averageWaitingTime(vector<vector<int>>& customers) {
114+
double tot = 0;
115+
int t = 0;
116+
for (auto& e : customers) {
117+
int a = e[0], b = e[1];
118+
t = max(t, a) + b;
119+
tot += t - a;
91120
}
92-
return totalWaitingTime / customers.length;
121+
return tot / customers.size();
93122
}
123+
};
124+
```
125+
126+
### **Go**
127+
128+
```go
129+
func averageWaitingTime(customers [][]int) float64 {
130+
tot, t := 0, 0
131+
for _, e := range customers {
132+
a, b := e[0], e[1]
133+
t = max(t, a) + b
134+
tot += t - a
135+
}
136+
return float64(tot) / float64(len(customers))
137+
}
138+
139+
func max(a, b int) int {
140+
if a > b {
141+
return a
142+
}
143+
return b
94144
}
95145
```
96146

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

Lines changed: 51 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -59,29 +59,69 @@ So the average waiting time = (2 + 6 + 4 + 1) / 4 = 3.25.
5959
```python
6060
class Solution:
6161
def averageWaitingTime(self, customers: List[List[int]]) -> float:
62-
f = total_waiting_time = 0
63-
for arrival, time in customers:
64-
f = max(arrival, f) + time
65-
total_waiting_time += f - arrival
66-
return total_waiting_time / len(customers)
62+
tot = t = 0
63+
for a, b in customers:
64+
t = max(t, a) + b
65+
tot += t - a
66+
return tot / len(customers)
6767
```
6868

6969
### **Java**
7070

7171
```java
7272
class Solution {
7373
public double averageWaitingTime(int[][] customers) {
74-
int f = 0;
75-
double totalWaitingTime = 0;
76-
for (int[] customer : customers) {
77-
f = Math.max(f, customer[0]) + customer[1];
78-
totalWaitingTime += (f - customer[0]);
74+
double tot = 0;
75+
int t = 0;
76+
for (var e : customers) {
77+
int a = e[0], b = e[1];
78+
t = Math.max(t, a) + b;
79+
tot += t - a;
7980
}
80-
return totalWaitingTime / customers.length;
81+
return tot / customers.length;
8182
}
8283
}
8384
```
8485

86+
### **C++**
87+
88+
```cpp
89+
class Solution {
90+
public:
91+
double averageWaitingTime(vector<vector<int>>& customers) {
92+
double tot = 0;
93+
int t = 0;
94+
for (auto& e : customers) {
95+
int a = e[0], b = e[1];
96+
t = max(t, a) + b;
97+
tot += t - a;
98+
}
99+
return tot / customers.size();
100+
}
101+
};
102+
```
103+
104+
### **Go**
105+
106+
```go
107+
func averageWaitingTime(customers [][]int) float64 {
108+
tot, t := 0, 0
109+
for _, e := range customers {
110+
a, b := e[0], e[1]
111+
t = max(t, a) + b
112+
tot += t - a
113+
}
114+
return float64(tot) / float64(len(customers))
115+
}
116+
117+
func max(a, b int) int {
118+
if a > b {
119+
return a
120+
}
121+
return b
122+
}
123+
```
124+
85125
### **...**
86126

87127
```
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
class Solution {
2+
public:
3+
double averageWaitingTime(vector<vector<int>>& customers) {
4+
double tot = 0;
5+
int t = 0;
6+
for (auto& e : customers) {
7+
int a = e[0], b = e[1];
8+
t = max(t, a) + b;
9+
tot += t - a;
10+
}
11+
return tot / customers.size();
12+
}
13+
};
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
func averageWaitingTime(customers [][]int) float64 {
2+
tot, t := 0, 0
3+
for _, e := range customers {
4+
a, b := e[0], e[1]
5+
t = max(t, a) + b
6+
tot += t - a
7+
}
8+
return float64(tot) / float64(len(customers))
9+
}
10+
11+
func max(a, b int) int {
12+
if a > b {
13+
return a
14+
}
15+
return b
16+
}
Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
class Solution {
22
public double averageWaitingTime(int[][] customers) {
3-
int f = 0;
4-
double totalWaitingTime = 0;
5-
for (int[] customer : customers) {
6-
f = Math.max(f, customer[0]) + customer[1];
7-
totalWaitingTime += (f - customer[0]);
3+
double tot = 0;
4+
int t = 0;
5+
for (var e : customers) {
6+
int a = e[0], b = e[1];
7+
t = Math.max(t, a) + b;
8+
tot += t - a;
89
}
9-
return totalWaitingTime / customers.length;
10+
return tot / customers.length;
1011
}
1112
}
Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
class Solution:
22
def averageWaitingTime(self, customers: List[List[int]]) -> float:
3-
f = total_waiting_time = 0
4-
for arrival, time in customers:
5-
f = max(arrival, f) + time
6-
total_waiting_time += f - arrival
7-
return total_waiting_time / len(customers)
3+
tot = t = 0
4+
for a, b in customers:
5+
t = max(t, a) + b
6+
tot += t - a
7+
return tot / len(customers)

0 commit comments

Comments
 (0)