diff --git a/leetcode/1301-1400/1353.Maximum-Number-of-Events-That-Can-Be-Attended/1.png b/leetcode/1301-1400/1353.Maximum-Number-of-Events-That-Can-Be-Attended/1.png new file mode 100644 index 000000000..619e0f483 Binary files /dev/null and b/leetcode/1301-1400/1353.Maximum-Number-of-Events-That-Can-Be-Attended/1.png differ diff --git a/leetcode/1301-1400/1353.Maximum-Number-of-Events-That-Can-Be-Attended/README.md b/leetcode/1301-1400/1353.Maximum-Number-of-Events-That-Can-Be-Attended/README.md index 8e41f3931..1f23238a8 100644 --- a/leetcode/1301-1400/1353.Maximum-Number-of-Events-That-Can-Be-Attended/README.md +++ b/leetcode/1301-1400/1353.Maximum-Number-of-Events-That-Can-Be-Attended/README.md @@ -1,28 +1,32 @@ # [1353.Maximum Number of Events That Can Be Attended][title] -> [!WARNING|style:flat] -> This question is temporarily unanswered if you have good ideas. Welcome to [Create Pull Request PR](https://github.com/kylesliu/awesome-golang-algorithm) - ## Description +You are given an array of `events` where `events[i] = [startDayi, endDayi]`. Every event `i` starts at `startDayi` and ends at `endDayi`. -**Example 1:** +You can attend an event `i` at any day `d` where `startTimei <= d <= endTimei`. You can only attend one event at any time `d`. -``` -Input: a = "11", b = "1" -Output: "100" -``` +Return the maximum number of events you can attend. -## 题意 -> ... +**Example 1:** -## 题解 +![1](./1.png) -### 思路1 -> ... -Maximum Number of Events That Can Be Attended -```go +``` +Input: events = [[1,2],[2,3],[3,4]] +Output: 3 +Explanation: You can attend all the three events. +One way to attend them all is as shown. +Attend the first event on day 1. +Attend the second event on day 2. +Attend the third event on day 3. ``` +**Example 2:** + +``` +Input: events= [[1,2],[2,3],[3,4],[1,2]] +Output: 4 +``` ## 结语 diff --git a/leetcode/1301-1400/1353.Maximum-Number-of-Events-That-Can-Be-Attended/Solution.go b/leetcode/1301-1400/1353.Maximum-Number-of-Events-That-Can-Be-Attended/Solution.go index d115ccf5e..bc066669a 100644 --- a/leetcode/1301-1400/1353.Maximum-Number-of-Events-That-Can-Be-Attended/Solution.go +++ b/leetcode/1301-1400/1353.Maximum-Number-of-Events-That-Can-Be-Attended/Solution.go @@ -1,5 +1,62 @@ package Solution -func Solution(x bool) bool { +import ( + "container/heap" + "sort" +) + +func Solution(events [][]int) int { + n := len(events) + maxDay := 0 + for _, event := range events { + if event[1] > maxDay { + maxDay = event[1] + } + } + sort.Slice(events, func(i, j int) bool { + return events[i][0] < events[j][0] + }) + pq := &IntHeap{} + heap.Init(pq) + ans := 0 + for i, j := 1, 0; i <= maxDay; i++ { + for j < n && events[j][0] <= i { + heap.Push(pq, events[j][1]) + j++ + } + for pq.Len() > 0 && (*pq)[0] < i { + heap.Pop(pq) + } + if pq.Len() > 0 { + heap.Pop(pq) + ans++ + } + } + return ans +} + +type IntHeap []int + +func (h IntHeap) Len() int { + return len(h) +} + +func (h IntHeap) Less(i, j int) bool { + return h[i] < h[j] +} + +func (h IntHeap) Swap(i, j int) { + h[i], h[j] = h[j], h[i] +} + +func (h *IntHeap) Push(x any) { + *h = append(*h, x.(int)) +} + +func (h *IntHeap) Pop() any { + old := *h + n := len(old) + x := old[n-1] + *h = old[0 : n-1] return x } diff --git a/leetcode/1301-1400/1353.Maximum-Number-of-Events-That-Can-Be-Attended/Solution_test.go b/leetcode/1301-1400/1353.Maximum-Number-of-Events-That-Can-Be-Attended/Solution_test.go index 14ff50eb4..da616ba7e 100644 --- a/leetcode/1301-1400/1353.Maximum-Number-of-Events-That-Can-Be-Attended/Solution_test.go +++ b/leetcode/1301-1400/1353.Maximum-Number-of-Events-That-Can-Be-Attended/Solution_test.go @@ -10,12 +10,11 @@ func TestSolution(t *testing.T) { // 测试用例 cases := []struct { name string - inputs bool - expect bool + inputs [][]int + expect int }{ - {"TestCase", true, true}, - {"TestCase", true, true}, - {"TestCase", false, false}, + {"TestCase1", [][]int{{1, 2}, {2, 3}, {3, 4}}, 3}, + {"TestCase2", [][]int{{1, 2}, {2, 3}, {3, 4}, {1, 3}}, 4}, } // 开始测试 @@ -30,10 +29,10 @@ func TestSolution(t *testing.T) { } } -// 压力测试 +// 压力测试 func BenchmarkSolution(b *testing.B) { } -// 使用案列 +// 使用案列 func ExampleSolution() { }