Skip to content

Add solution and test-cases for problem 3440 #1257

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
@@ -1,28 +1,71 @@
# [3440.Reschedule Meetings for Maximum Free Time II][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 integer `eventTime` denoting the duration of an event. You are also given two integer arrays `startTime` and `endTime`, each of length `n`.

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]]`.

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.

Return the **maximum** amount of free time possible after rearranging the meetings.

**Note** that the meetings can **not** be rescheduled to a time outside the event and they should remain non-overlapping.

**Note**: In this version, it is **valid** for the relative ordering of the meetings to change after rescheduling one meeting.

**Example 1:**

![1](./1.png)

```
Input: eventTime = 5, startTime = [1,3], endTime = [2,5]

Output: 2

Explanation:

Reschedule the meeting at [1, 2] to [2, 3], leaving no meetings during the time [0, 2].
```

**Example 1:**
**Example 2:**

![2](./2.png)

```
Input: a = "11", b = "1"
Output: "100"
Input: eventTime = 10, startTime = [0,7,9], endTime = [1,8,10]

Output: 7

Explanation:

Reschedule the meeting at [0, 1] to [8, 9], leaving no meetings during the time [0, 7].
```

**Example 3:**

![3](./3.png)

```
Input: eventTime = 10, startTime = [0,3,7,9], endTime = [1,4,8,10]

Output: 6

## 题意
> ...
Explanation:

Reschedule the meeting at [3, 4] to [8, 9], leaving no meetings during the time [1, 7].
```

## 题解
**Example 4:**

### 思路1
> ...
Reschedule Meetings for Maximum Free Time II
```go
```
Input: eventTime = 5, startTime = [0,1,2,3,4], endTime = [1,2,3,4,5]

Output: 0

Explanation:

There is no time during the event not occupied by meetings.
```

## 结语

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,44 @@
package Solution

func Solution(x bool) bool {
return x
func Solution(eventTime int, startTime []int, endTime []int) int {
n := len(startTime)
q := make([]bool, n)
t1, t2 := 0, 0
for i := 0; i < n; i++ {
if endTime[i]-startTime[i] <= t1 {
q[i] = true
}
if i == 0 {
t1 = max(t1, startTime[i])
} else {
t1 = max(t1, startTime[i]-endTime[i-1])
}

if endTime[n-i-1]-startTime[n-i-1] <= t2 {
q[n-i-1] = true
}
if i == 0 {
t2 = max(t2, eventTime-endTime[n-1])
} else {
t2 = max(t2, startTime[n-i]-endTime[n-i-1])
}
}

res := 0
for i := 0; i < n; i++ {
left := 0
if i != 0 {
left = endTime[i-1]
}
right := eventTime
if i != n-1 {
right = startTime[i+1]
}
if q[i] {
res = max(res, right-left)
} else {
res = max(res, right-left-(endTime[i]-startTime[i]))
}
}
return res
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,31 +9,32 @@ import (
func TestSolution(t *testing.T) {
// 测试用例
cases := []struct {
name string
inputs bool
expect bool
name string
eventTime int
startTime, endTime []int
expect int
}{
{"TestCase", true, true},
{"TestCase", true, true},
{"TestCase", false, false},
{"TestCase1", 5, []int{1, 3}, []int{2, 5}, 2},
{"TestCase2", 10, []int{0, 7, 9}, []int{1, 8, 10}, 7},
{"TestCase3", 10, []int{0, 3, 7, 9}, []int{1, 4, 8, 10}, 6},
}

// 开始测试
for i, c := range cases {
t.Run(c.name+" "+strconv.Itoa(i), func(t *testing.T) {
got := Solution(c.inputs)
got := Solution(c.eventTime, c.startTime, c.endTime)
if !reflect.DeepEqual(got, c.expect) {
t.Fatalf("expected: %v, but got: %v, with inputs: %v",
c.expect, got, c.inputs)
t.Fatalf("expected: %v, but got: %v, with inputs: %v %v %v",
c.expect, got, c.eventTime, c.startTime, c.endTime)
}
})
}
}

// 压力测试
// 压力测试
func BenchmarkSolution(b *testing.B) {
}

// 使用案列
// 使用案列
func ExampleSolution() {
}
Loading