Skip to content

Commit ef44940

Browse files
authored
Merge pull request #839 from 0xff-dev/2073
Add solution and test-cases for problem 2073
2 parents 138a64e + 380f62f commit ef44940

File tree

3 files changed

+44
-26
lines changed

3 files changed

+44
-26
lines changed

leetcode/2001-2100/2073.Time-Needed-to-Buy-Tickets/README.md

Lines changed: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,35 @@
11
# [2073.Time Needed to Buy Tickets][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 are `n` people in a line queuing to buy tickets, where the 0<sup>th</sup> person is at the **front** of the line and the (n - 1)<sup>th</sup> person is at the **back** of the line.
5+
6+
You are given a **0-indexed** integer array `tickets` of length `n` where the number of tickets that the i<sup>th</sup> person would like to buy is `tickets[i]`.
7+
8+
Each person takes **exactly 1 second** to buy a ticket. A person can only buy **1 ticket at a time** and has to go back to **the end** of the line (which happens **instantaneously**) in order to buy more tickets. If a person does not have any tickets left to buy, the person will **leave** the line.
9+
10+
Return the **time taken** for the person at position k (**0-indexed**) to finish buying tickets.
711

812
**Example 1:**
913

1014
```
11-
Input: a = "11", b = "1"
12-
Output: "100"
15+
Input: tickets = [2,3,2], k = 2
16+
Output: 6
17+
Explanation:
18+
- In the first pass, everyone in the line buys a ticket and the line becomes [1, 2, 1].
19+
- In the second pass, everyone in the line buys a ticket and the line becomes [0, 1, 0].
20+
The person at position 2 has successfully bought 2 tickets and it took 3 + 3 = 6 seconds.
1321
```
1422

15-
## 题意
16-
> ...
23+
**Example 2:**
1724

18-
## 题解
19-
20-
### 思路1
21-
> ...
22-
Time Needed to Buy Tickets
23-
```go
2425
```
25-
26+
Input: tickets = [5,1,1,1], k = 0
27+
Output: 8
28+
Explanation:
29+
- In the first pass, everyone in the line buys a ticket and the line becomes [4, 0, 0, 0].
30+
- In the next 4 passes, only the person in position 0 is buying tickets.
31+
The person at position 0 has successfully bought 5 tickets and it took 4 + 1 + 1 + 1 + 1 = 8 seconds.
32+
```
2633

2734
## 结语
2835

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

3-
func Solution(x bool) bool {
4-
return x
3+
func Solution(tickets []int, k int) int {
4+
// 每个人1s的轮下来
5+
// 对于左侧的人,和右侧的人有差异,差异点在于:最后一轮到k后,右侧就不需要再走了
6+
// 所以最后从整体看就是,左侧:min(tickets[k], ticket[i]), 右侧就是min(tickets[k]-1,tickets[i])
7+
time := 0
8+
for i := 0; i < len(tickets); i++ {
9+
if i <= k {
10+
time += min(tickets[i], tickets[k])
11+
} else {
12+
time += min(tickets[k]-1, tickets[i])
13+
}
14+
}
15+
return time
516
}

leetcode/2001-2100/2073.Time-Needed-to-Buy-Tickets/Solution_test.go

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,30 +10,30 @@ func TestSolution(t *testing.T) {
1010
// 测试用例
1111
cases := []struct {
1212
name string
13-
inputs bool
14-
expect bool
13+
inputs []int
14+
k int
15+
expect int
1516
}{
16-
{"TestCase", true, true},
17-
{"TestCase", true, true},
18-
{"TestCase", false, false},
17+
{"TestCase1", []int{2, 3, 2}, 2, 6},
18+
{"TestCase2", []int{5, 1, 1, 1}, 0, 8},
1919
}
2020

2121
// 开始测试
2222
for i, c := range cases {
2323
t.Run(c.name+" "+strconv.Itoa(i), func(t *testing.T) {
24-
got := Solution(c.inputs)
24+
got := Solution(c.inputs, c.k)
2525
if !reflect.DeepEqual(got, c.expect) {
26-
t.Fatalf("expected: %v, but got: %v, with inputs: %v",
27-
c.expect, got, c.inputs)
26+
t.Fatalf("expected: %v, but got: %v, with inputs: %v 5v",
27+
c.expect, got, c.inputs, c.k)
2828
}
2929
})
3030
}
3131
}
3232

33-
// 压力测试
33+
// 压力测试
3434
func BenchmarkSolution(b *testing.B) {
3535
}
3636

37-
// 使用案列
37+
// 使用案列
3838
func ExampleSolution() {
3939
}

0 commit comments

Comments
 (0)