Skip to content

Commit 0a145fc

Browse files
authored
Merge pull request #1257 from 0xff-dev/3440
Add solution and test-cases for problem 3440
2 parents 8383282 + 060d775 commit 0a145fc

File tree

6 files changed

+109
-26
lines changed

6 files changed

+109
-26
lines changed
Loading
Loading
Loading

leetcode/3401-3500/3440.Reschedule-Meetings-for-Maximum-Free-Time-II/README.md

Lines changed: 56 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,71 @@
11
# [3440.Reschedule Meetings for Maximum Free Time II][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 integer `eventTime` denoting the duration of an event. You are also given two integer arrays `startTime` and `endTime`, each of length `n`.
5+
6+
These represent the start and end times of `n` **non-overlapping** meetings that occur during the event between time `t = 0` and time `t = eventTime`, where the `ith` meeting occurs during the time `[startTime[i], endTime[i]]`.
7+
8+
You can reschedule **at most** one meeting by moving its start time while maintaining the **same duration**, such that the meetings remain non-overlapping, to **maximize** the **longest** continuous period of free time during the event.
9+
10+
Return the **maximum** amount of free time possible after rearranging the meetings.
11+
12+
**Note** that the meetings can **not** be rescheduled to a time outside the event and they should remain non-overlapping.
13+
14+
**Note**: In this version, it is **valid** for the relative ordering of the meetings to change after rescheduling one meeting.
15+
16+
**Example 1:**
17+
18+
![1](./1.png)
19+
20+
```
21+
Input: eventTime = 5, startTime = [1,3], endTime = [2,5]
22+
23+
Output: 2
24+
25+
Explanation:
26+
27+
Reschedule the meeting at [1, 2] to [2, 3], leaving no meetings during the time [0, 2].
28+
```
729

8-
**Example 1:**
30+
**Example 2:**
31+
32+
![2](./2.png)
933

1034
```
11-
Input: a = "11", b = "1"
12-
Output: "100"
35+
Input: eventTime = 10, startTime = [0,7,9], endTime = [1,8,10]
36+
37+
Output: 7
38+
39+
Explanation:
40+
41+
Reschedule the meeting at [0, 1] to [8, 9], leaving no meetings during the time [0, 7].
42+
```
43+
44+
**Example 3:**
45+
46+
![3](./3.png)
47+
1348
```
49+
Input: eventTime = 10, startTime = [0,3,7,9], endTime = [1,4,8,10]
50+
51+
Output: 6
1452
15-
## 题意
16-
> ...
53+
Explanation:
54+
55+
Reschedule the meeting at [3, 4] to [8, 9], leaving no meetings during the time [1, 7].
56+
```
1757

18-
## 题解
58+
**Example 4:**
1959

20-
### 思路1
21-
> ...
22-
Reschedule Meetings for Maximum Free Time II
23-
```go
2460
```
61+
Input: eventTime = 5, startTime = [0,1,2,3,4], endTime = [1,2,3,4,5]
2562
63+
Output: 0
64+
65+
Explanation:
66+
67+
There is no time during the event not occupied by meetings.
68+
```
2669

2770
## 结语
2871

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

3-
func Solution(x bool) bool {
4-
return x
3+
func Solution(eventTime int, startTime []int, endTime []int) int {
4+
n := len(startTime)
5+
q := make([]bool, n)
6+
t1, t2 := 0, 0
7+
for i := 0; i < n; i++ {
8+
if endTime[i]-startTime[i] <= t1 {
9+
q[i] = true
10+
}
11+
if i == 0 {
12+
t1 = max(t1, startTime[i])
13+
} else {
14+
t1 = max(t1, startTime[i]-endTime[i-1])
15+
}
16+
17+
if endTime[n-i-1]-startTime[n-i-1] <= t2 {
18+
q[n-i-1] = true
19+
}
20+
if i == 0 {
21+
t2 = max(t2, eventTime-endTime[n-1])
22+
} else {
23+
t2 = max(t2, startTime[n-i]-endTime[n-i-1])
24+
}
25+
}
26+
27+
res := 0
28+
for i := 0; i < n; i++ {
29+
left := 0
30+
if i != 0 {
31+
left = endTime[i-1]
32+
}
33+
right := eventTime
34+
if i != n-1 {
35+
right = startTime[i+1]
36+
}
37+
if q[i] {
38+
res = max(res, right-left)
39+
} else {
40+
res = max(res, right-left-(endTime[i]-startTime[i]))
41+
}
42+
}
43+
return res
544
}

leetcode/3401-3500/3440.Reschedule-Meetings-for-Maximum-Free-Time-II/Solution_test.go

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,31 +9,32 @@ import (
99
func TestSolution(t *testing.T) {
1010
// 测试用例
1111
cases := []struct {
12-
name string
13-
inputs bool
14-
expect bool
12+
name string
13+
eventTime int
14+
startTime, endTime []int
15+
expect int
1516
}{
16-
{"TestCase", true, true},
17-
{"TestCase", true, true},
18-
{"TestCase", false, false},
17+
{"TestCase1", 5, []int{1, 3}, []int{2, 5}, 2},
18+
{"TestCase2", 10, []int{0, 7, 9}, []int{1, 8, 10}, 7},
19+
{"TestCase3", 10, []int{0, 3, 7, 9}, []int{1, 4, 8, 10}, 6},
1920
}
2021

2122
// 开始测试
2223
for i, c := range cases {
2324
t.Run(c.name+" "+strconv.Itoa(i), func(t *testing.T) {
24-
got := Solution(c.inputs)
25+
got := Solution(c.eventTime, c.startTime, c.endTime)
2526
if !reflect.DeepEqual(got, c.expect) {
26-
t.Fatalf("expected: %v, but got: %v, with inputs: %v",
27-
c.expect, got, c.inputs)
27+
t.Fatalf("expected: %v, but got: %v, with inputs: %v %v %v",
28+
c.expect, got, c.eventTime, c.startTime, c.endTime)
2829
}
2930
})
3031
}
3132
}
3233

33-
// 压力测试
34+
// 压力测试
3435
func BenchmarkSolution(b *testing.B) {
3536
}
3637

37-
// 使用案列
38+
// 使用案列
3839
func ExampleSolution() {
3940
}

0 commit comments

Comments
 (0)