Skip to content

Commit 4f295b7

Browse files
authored
Merge pull request #929 from 0xff-dev/1701
Add solution and test-cases for problem 1701
2 parents 2fe43dc + a31c795 commit 4f295b7

File tree

3 files changed

+55
-25
lines changed

3 files changed

+55
-25
lines changed

leetcode/1701-1800/1701.Average-Waiting-Time/README.md

Lines changed: 25 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,39 @@
11
# [1701.Average Waiting Time][title]
22

3-
> [!WARNING|style:flat]
4-
> This question is temporarily unanswered if you have good ideas. Welcome to [Create Pull Request PR](https://github.com/kylesliu/awesome-golang-algorithm)
5-
63
## Description
4+
There is a restaurant with a single chef. You are given an array `customers`, where `customers[i] = [arrivali, timei]`:
5+
6+
- `arrivali` is the arrival time of the i<sup>th</sup> customer. The arrival times are sorted in **non-decreasing** order.
7+
- `timei` is the time needed to prepare the order of the i<sup>th</sup> customer.
8+
9+
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**.
10+
11+
Return the **average** waiting time of all customers. Solutions within `10^-5` from the actual answer are considered accepted.
712

813
**Example 1:**
914

1015
```
11-
Input: a = "11", b = "1"
12-
Output: "100"
16+
Input: customers = [[1,2],[2,5],[4,3]]
17+
Output: 5.00000
18+
Explanation:
19+
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.
20+
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.
21+
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.
22+
So the average waiting time = (2 + 6 + 7) / 3 = 5.
1323
```
1424

15-
## 题意
16-
> ...
25+
**Example 2:**
1726

18-
## 题解
19-
20-
### 思路1
21-
> ...
22-
Average Waiting Time
23-
```go
2427
```
25-
28+
Input: customers = [[5,2],[5,4],[10,3],[20,1]]
29+
Output: 3.25000
30+
Explanation:
31+
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.
32+
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.
33+
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.
34+
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.
35+
So the average waiting time = (2 + 6 + 4 + 1) / 4 = 3.25.
36+
```
2637

2738
## 结语
2839

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,14 @@
11
package Solution
22

3-
func Solution(x bool) bool {
4-
return x
3+
func Solution(customers [][]int) float64 {
4+
sum, end := 0, 0
5+
for _, c := range customers {
6+
sum += c[1]
7+
if c[0] < end {
8+
sum += end - c[0]
9+
}
10+
11+
end = max(end+c[1], c[0]+c[1])
12+
}
13+
return float64(sum) / float64(len(customers))
514
}
Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,49 @@
11
package Solution
22

33
import (
4-
"reflect"
54
"strconv"
65
"testing"
76
)
87

8+
const (
9+
within = 1e-5
10+
)
11+
912
func TestSolution(t *testing.T) {
1013
// 测试用例
1114
cases := []struct {
1215
name string
13-
inputs bool
14-
expect bool
16+
inputs [][]int
17+
expect float64
1518
}{
16-
{"TestCase", true, true},
17-
{"TestCase", true, true},
18-
{"TestCase", false, false},
19+
{"TestCase1", [][]int{
20+
{1, 2}, {2, 5}, {4, 3},
21+
}, 5.00000},
22+
{"TestCase2", [][]int{
23+
{5, 2}, {5, 4}, {10, 3}, {20, 1},
24+
}, 3.25000},
1925
}
2026

2127
// 开始测试
2228
for i, c := range cases {
2329
t.Run(c.name+" "+strconv.Itoa(i), func(t *testing.T) {
2430
got := Solution(c.inputs)
25-
if !reflect.DeepEqual(got, c.expect) {
31+
diff := got - c.expect
32+
if diff < 0 {
33+
diff = -diff
34+
}
35+
if diff > within {
2636
t.Fatalf("expected: %v, but got: %v, with inputs: %v",
2737
c.expect, got, c.inputs)
2838
}
2939
})
3040
}
3141
}
3242

33-
// 压力测试
43+
// 压力测试
3444
func BenchmarkSolution(b *testing.B) {
3545
}
3646

37-
// 使用案列
47+
// 使用案列
3848
func ExampleSolution() {
3949
}

0 commit comments

Comments
 (0)