Skip to content

Commit 322ffde

Browse files
authored
Merge pull request #1256 from 0xff-dev/3439
Add solution and test-cases for problem 3439
2 parents 0a145fc + 89de3c6 commit 322ffde

File tree

5 files changed

+77
-26
lines changed

5 files changed

+77
-26
lines changed
Loading
Loading

leetcode/3401-3500/3439.Reschedule-Meetings-for-Maximum-Free-Time-I/README.md

Lines changed: 42 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,57 @@
11
# [3439.Reschedule Meetings for Maximum Free Time I][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, where the event occurs from time `t = 0` to time `t = eventTime`.
5+
6+
You are also given two integer arrays `startTime` and `endTime`, each of length `n`. These represent the start and end time of `n` **non-overlapping** meetings, where the `ith` meeting occurs during the time `[startTime[i], endTime[i]]`.
7+
8+
You can reschedule **at most** `k` meetings by moving their start time while maintaining the **same duration**, to **maximize** the **longest** continuous period of free time during the event.
9+
10+
The **relative** order of all the meetings should stay the same and they should remain non-overlapping.
11+
12+
Return the **maximum** amount of free time possible after rearranging the meetings.
13+
14+
**Note** that the meetings can **not** be rescheduled to a time outside the event.
15+
16+
**Example 1:**
17+
18+
![1](./1.png)
19+
20+
```
21+
Input: eventTime = 5, k = 1, startTime = [1,3], endTime = [2,5]
722
8-
**Example 1:**
23+
Output: 2
924
25+
Explanation:
26+
27+
Reschedule the meeting at [1, 2] to [2, 3], leaving no meetings during the time [0, 2].
1028
```
11-
Input: a = "11", b = "1"
12-
Output: "100"
29+
30+
**Example 2:**
31+
32+
![2](./2.png)
33+
1334
```
35+
Input: eventTime = 10, k = 1, startTime = [0,2,9], endTime = [1,4,10]
36+
37+
Output: 6
1438
15-
## 题意
16-
> ...
39+
Explanation:
1740
18-
## 题解
41+
Reschedule the meeting at [2, 4] to [1, 3], leaving no meetings during the time [3, 9].
42+
```
43+
44+
**Example 3:**
1945

20-
### 思路1
21-
> ...
22-
Reschedule Meetings for Maximum Free Time I
23-
```go
2446
```
47+
Input: eventTime = 5, k = 2, startTime = [0,1,2,3,4], endTime = [1,2,3,4,5]
48+
49+
Output: 0
2550
51+
Explanation:
52+
53+
There is no time during the event not occupied by meetings
54+
```
2655

2756
## 结语
2857

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

3-
func Solution(x bool) bool {
4-
return x
3+
func Solution(eventTime int, k int, startTime []int, endTime []int) int {
4+
n := len(startTime)
5+
res := 0
6+
sum := make([]int, n+1)
7+
for i := 0; i < n; i++ {
8+
sum[i+1] = sum[i] + endTime[i] - startTime[i]
9+
}
10+
for i := k - 1; i < n; i++ {
11+
var right int
12+
if i == n-1 {
13+
right = eventTime
14+
} else {
15+
right = startTime[i+1]
16+
}
17+
var left int
18+
if i == k-1 {
19+
left = 0
20+
} else {
21+
left = endTime[i-k]
22+
}
23+
res = max(res, right-left-(sum[i+1]-sum[i-k+1]))
24+
}
25+
return res
526
}

leetcode/3401-3500/3439.Reschedule-Meetings-for-Maximum-Free-Time-I/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, k int
14+
startTime, endTime []int
15+
expect int
1516
}{
16-
{"TestCase", true, true},
17-
{"TestCase", true, true},
18-
{"TestCase", false, false},
17+
{"TestCase1", 5, 1, []int{1, 3}, []int{2, 5}, 2},
18+
{"TestCase2", 10, 1, []int{0, 2, 9}, []int{1, 4, 10}, 6},
19+
{"TestCase3", 5, 2, []int{0, 1, 2, 3, 4}, []int{1, 2, 3, 4, 5}, 0},
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.k, 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 %v",
28+
c.expect, got, c.eventTime, c.k, 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)