Skip to content

Commit a1c271a

Browse files
authored
Merge pull request #1254 from 0xff-dev/1353
Add solution and test-cases for problem 1353
2 parents 28caba2 + b3d60cc commit a1c271a

File tree

4 files changed

+83
-23
lines changed

4 files changed

+83
-23
lines changed
8.9 KB
Loading

leetcode/1301-1400/1353.Maximum-Number-of-Events-That-Can-Be-Attended/README.md

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,32 @@
11
# [1353.Maximum Number of Events That Can Be Attended][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+
You are given an array of `events` where `events[i] = [startDayi, endDayi]`. Every event `i` starts at `startDayi` and ends at `endDayi`.
75

8-
**Example 1:**
6+
You can attend an event `i` at any day `d` where `startTimei <= d <= endTimei`. You can only attend one event at any time `d`.
97

10-
```
11-
Input: a = "11", b = "1"
12-
Output: "100"
13-
```
8+
Return the maximum number of events you can attend.
149

15-
## 题意
16-
> ...
10+
**Example 1:**
1711

18-
## 题解
12+
![1](./1.png)
1913

20-
### 思路1
21-
> ...
22-
Maximum Number of Events That Can Be Attended
23-
```go
14+
```
15+
Input: events = [[1,2],[2,3],[3,4]]
16+
Output: 3
17+
Explanation: You can attend all the three events.
18+
One way to attend them all is as shown.
19+
Attend the first event on day 1.
20+
Attend the second event on day 2.
21+
Attend the third event on day 3.
2422
```
2523

24+
**Example 2:**
25+
26+
```
27+
Input: events= [[1,2],[2,3],[3,4],[1,2]]
28+
Output: 4
29+
```
2630

2731
## 结语
2832

Lines changed: 58 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,62 @@
11
package Solution
22

3-
func Solution(x bool) bool {
3+
import (
4+
"container/heap"
5+
"sort"
6+
)
7+
8+
func Solution(events [][]int) int {
9+
n := len(events)
10+
maxDay := 0
11+
for _, event := range events {
12+
if event[1] > maxDay {
13+
maxDay = event[1]
14+
}
15+
}
16+
sort.Slice(events, func(i, j int) bool {
17+
return events[i][0] < events[j][0]
18+
})
19+
pq := &IntHeap{}
20+
heap.Init(pq)
21+
ans := 0
22+
for i, j := 1, 0; i <= maxDay; i++ {
23+
for j < n && events[j][0] <= i {
24+
heap.Push(pq, events[j][1])
25+
j++
26+
}
27+
for pq.Len() > 0 && (*pq)[0] < i {
28+
heap.Pop(pq)
29+
}
30+
if pq.Len() > 0 {
31+
heap.Pop(pq)
32+
ans++
33+
}
34+
}
35+
return ans
36+
}
37+
38+
type IntHeap []int
39+
40+
func (h IntHeap) Len() int {
41+
return len(h)
42+
}
43+
44+
func (h IntHeap) Less(i, j int) bool {
45+
return h[i] < h[j]
46+
}
47+
48+
func (h IntHeap) Swap(i, j int) {
49+
h[i], h[j] = h[j], h[i]
50+
}
51+
52+
func (h *IntHeap) Push(x any) {
53+
*h = append(*h, x.(int))
54+
}
55+
56+
func (h *IntHeap) Pop() any {
57+
old := *h
58+
n := len(old)
59+
x := old[n-1]
60+
*h = old[0 : n-1]
461
return x
562
}

leetcode/1301-1400/1353.Maximum-Number-of-Events-That-Can-Be-Attended/Solution_test.go

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,11 @@ func TestSolution(t *testing.T) {
1010
// 测试用例
1111
cases := []struct {
1212
name string
13-
inputs bool
14-
expect bool
13+
inputs [][]int
14+
expect int
1515
}{
16-
{"TestCase", true, true},
17-
{"TestCase", true, true},
18-
{"TestCase", false, false},
16+
{"TestCase1", [][]int{{1, 2}, {2, 3}, {3, 4}}, 3},
17+
{"TestCase2", [][]int{{1, 2}, {2, 3}, {3, 4}, {1, 3}}, 4},
1918
}
2019

2120
// 开始测试
@@ -30,10 +29,10 @@ func TestSolution(t *testing.T) {
3029
}
3130
}
3231

33-
// 压力测试
32+
// 压力测试
3433
func BenchmarkSolution(b *testing.B) {
3534
}
3635

37-
// 使用案列
36+
// 使用案列
3837
func ExampleSolution() {
3938
}

0 commit comments

Comments
 (0)