Skip to content

Commit d5cb9b2

Browse files
authored
Merge pull request #1055 from 0xff-dev/2054
Add solution and test-cases for problem 2054
2 parents 25b4090 + 68e54cb commit d5cb9b2

File tree

6 files changed

+88
-22
lines changed

6 files changed

+88
-22
lines changed
4.15 KB
Loading
4 KB
Loading
4.96 KB
Loading

leetcode/2001-2100/2054.Two-Best-Non-Overlapping-Events/README.md

Lines changed: 26 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,41 @@
11
# [2054.Two Best Non-Overlapping Events][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 a **0-indexed** 2D integer array of `events` where `events[i] = [startTimei, endTimei, valuei]`. The `ith` event starts at `startTimei` and ends at `endTimei`, and if you attend this event, you will receive a value of `valuei`. You can choose **at most two non-overlapping** events to attend such that the sum of their values is **maximized**.
5+
6+
Return this **maximum** sum.
7+
8+
Note that the start time and end time is **inclusive**: that is, you cannot attend two events where one of them starts and the other ends at the same time. More specifically, if you attend an event with end time `t`, the next event must start at or after `t + 1`.
79

8-
**Example 1:**
10+
**Example 1:**
11+
12+
![1](./1.png)
913

1014
```
11-
Input: a = "11", b = "1"
12-
Output: "100"
15+
Input: events = [[1,3,2],[4,5,2],[2,4,3]]
16+
Output: 4
17+
Explanation: Choose the green events, 0 and 1 for a sum of 2 + 2 = 4.
1318
```
1419

15-
## 题意
16-
> ...
20+
**Example 2:**
1721

18-
## 题解
22+
![2](./2.png)
1923

20-
### 思路1
21-
> ...
22-
Two Best Non-Overlapping Events
23-
```go
24+
```
25+
Input: events = [[1,3,2],[4,5,2],[1,5,5]]
26+
Output: 5
27+
Explanation: Choose event 2 for a sum of 5.
2428
```
2529

30+
**Example 3:**
31+
32+
![3](./3.png)
33+
34+
```
35+
Input: events = [[1,5,3],[1,5,1],[6,6,5]]
36+
Output: 8
37+
Explanation: Choose events 0 and 2 for a sum of 3 + 5 = 8.
38+
```
2639

2740
## 结语
2841

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

3-
func Solution(x bool) bool {
4-
return x
3+
import "sort"
4+
5+
type SegmentTreeNode2054 struct {
6+
Left, Right, Max int
7+
LeftChild, RightChild *SegmentTreeNode2054
8+
}
9+
10+
func buildSegmentTree2054(nums [][]int, left, right int) *SegmentTreeNode2054 {
11+
if left == right {
12+
return &SegmentTreeNode2054{Left: left, Right: right, Max: nums[left][2]}
13+
}
14+
15+
mid := (left + right) / 2
16+
leftNode := buildSegmentTree2054(nums, left, mid)
17+
rightNode := buildSegmentTree2054(nums, mid+1, right)
18+
max := max(leftNode.Max, rightNode.Max)
19+
20+
return &SegmentTreeNode2054{Left: left, Right: right, Max: max, LeftChild: leftNode, RightChild: rightNode}
21+
}
22+
23+
func queryMax2054(root *SegmentTreeNode2054, left, right int) int {
24+
if root.Left >= left && root.Right <= right {
25+
return root.Max
26+
}
27+
28+
if root.Right < left || root.Left > right {
29+
return -1
30+
}
31+
32+
return max(queryMax2054(root.LeftChild, left, right), queryMax2054(root.RightChild, left, right))
33+
}
34+
35+
func Solution(events [][]int) int {
36+
sort.Slice(events, func(i, j int) bool {
37+
a, b := events[i], events[j]
38+
if a[0] == b[0] {
39+
return a[1] < b[1]
40+
}
41+
return a[0] < b[0]
42+
})
43+
l := len(events)
44+
45+
tree := buildSegmentTree2054(events, 0, l-1)
46+
ans := 0
47+
for i, e := range events {
48+
ans = max(ans, e[2])
49+
idx := sort.Search(l-i-1, func(ii int) bool {
50+
return events[i+1+ii][0] > e[1]
51+
})
52+
if idx == l-i-1 {
53+
continue
54+
}
55+
ans = max(ans, e[2]+queryMax2054(tree, i+1+idx, l-1))
56+
}
57+
return ans
558
}

leetcode/2001-2100/2054.Two-Best-Non-Overlapping-Events/Solution_test.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,12 @@ 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, 3, 2}, {4, 5, 2}, {2, 4, 3}}, 4},
17+
{"TestCase2", [][]int{{1, 3, 2}, {4, 5, 2}, {1, 5, 5}}, 5},
18+
{"TestCase3", [][]int{{1, 5, 3}, {1, 5, 1}, {6, 6, 5}}, 8},
1919
}
2020

2121
// 开始测试
@@ -30,10 +30,10 @@ func TestSolution(t *testing.T) {
3030
}
3131
}
3232

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

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

0 commit comments

Comments
 (0)