File tree 4 files changed +56
-6
lines changed
solution/1700-1799/1701.Average Waiting Time
4 files changed +56
-6
lines changed Original file line number Diff line number Diff line change 54
54
<li><code>arrival<sub>i </sub><= arrival<sub>i+1</sub></code></li>
55
55
</ul >
56
56
57
-
58
57
## 解法
59
58
60
59
<!-- 这里可写通用的实现逻辑 -->
61
60
61
+ 记 totalWaitingTime 表示总等待时间,f 表示当次做菜完成时间。
62
+
62
63
<!-- tabs:start -->
63
64
64
65
### ** Python3**
65
66
66
67
<!-- 这里可写当前语言的特殊实现逻辑 -->
67
68
68
69
``` python
69
-
70
+ class Solution :
71
+ 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)
70
77
```
71
78
72
79
### ** Java**
73
80
74
81
<!-- 这里可写当前语言的特殊实现逻辑 -->
75
82
76
83
``` java
77
-
84
+ class Solution {
85
+ 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 ]);
91
+ }
92
+ return totalWaitingTime / customers. length;
93
+ }
94
+ }
78
95
```
79
96
80
97
### ** ...**
Original file line number Diff line number Diff line change @@ -50,21 +50,36 @@ So the average waiting time = (2 + 6 + 4 + 1) / 4 = 3.25.
50
50
<li><code>arrival<sub>i </sub><= arrival<sub>i+1</sub></code></li>
51
51
</ul >
52
52
53
-
54
53
## Solutions
55
54
56
55
<!-- tabs:start -->
57
56
58
57
### ** Python3**
59
58
60
59
``` python
61
-
60
+ class Solution :
61
+ 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
67
```
63
68
64
69
### ** Java**
65
70
66
71
``` java
67
-
72
+ class Solution {
73
+ 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 ]);
79
+ }
80
+ return totalWaitingTime / customers. length;
81
+ }
82
+ }
68
83
```
69
84
70
85
### ** ...**
Original file line number Diff line number Diff line change
1
+ class Solution {
2
+ 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 ]);
8
+ }
9
+ return totalWaitingTime / customers .length ;
10
+ }
11
+ }
Original file line number Diff line number Diff line change
1
+ class Solution :
2
+ 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 )
You can’t perform that action at this time.
0 commit comments