Skip to content

Commit 23960d1

Browse files
committed
Add solution and test-cases for problem 3169
1 parent f0a9eb1 commit 23960d1

File tree

3 files changed

+63
-25
lines changed

3 files changed

+63
-25
lines changed

leetcode/3101-3200/3169.Count-Days-Without-Meetings/README.md

Lines changed: 31 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,47 @@
11
# [3169.Count Days Without Meetings][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 positive integer `days` representing the total number of days an employee is available for work (starting from day 1). You are also given a 2D array `meetings` of size `n` where, `meetings[i] = [start_i, end_i]` represents the starting and ending days of meeting `i` (inclusive).
5+
6+
Return the count of days when the employee is available for work but no meetings are scheduled.
7+
8+
**Note**: The meetings may overlap.
79

810
**Example 1:**
911

1012
```
11-
Input: a = "11", b = "1"
12-
Output: "100"
13+
Input: days = 10, meetings = [[5,7],[1,3],[9,10]]
14+
15+
Output: 2
16+
17+
Explanation:
18+
19+
There is no meeting scheduled on the 4th and 8th days.
1320
```
1421

15-
## 题意
16-
> ...
22+
**Example 2:**
23+
24+
```
25+
Input: days = 5, meetings = [[2,4],[1,3]]
1726
18-
## 题解
27+
Output: 1
28+
29+
Explanation:
30+
31+
There is no meeting scheduled on the 5th day.
32+
```
33+
34+
**Example 3:**
1935

20-
### 思路1
21-
> ...
22-
Count Days Without Meetings
23-
```go
2436
```
37+
Input: days = 6, meetings = [[1,6]]
2538
39+
Output: 0
40+
41+
Explanation:
42+
43+
Meetings are scheduled for all working days.
44+
```
2645

2746
## 结语
2847

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

3-
func Solution(x bool) bool {
4-
return x
3+
import "sort"
4+
5+
func Solution(days int, meetings [][]int) int {
6+
ans := 0
7+
sort.Slice(meetings, func(i, j int) bool {
8+
a, b := meetings[i], meetings[j]
9+
if a[0] == b[0] {
10+
return a[1] < b[1]
11+
}
12+
return a[0] < b[0]
13+
})
14+
end := 0
15+
for i := 0; i < len(meetings); i++ {
16+
if meetings[i][0] > end {
17+
ans += meetings[i][0] - end - 1
18+
}
19+
end = max(end, meetings[i][1])
20+
}
21+
ans += days - end
22+
return ans
523
}

leetcode/3101-3200/3169.Count-Days-Without-Meetings/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+
days int
14+
meetings [][]int
15+
expect int
1516
}{
16-
{"TestCase", true, true},
17-
{"TestCase", true, true},
18-
{"TestCase", false, false},
17+
{"TestCase1", 10, [][]int{{5, 7}, {1, 3}, {9, 10}}, 2},
18+
{"TestCase2", 5, [][]int{{2, 4}, {1, 3}}, 1},
19+
{"TestCase3", 6, [][]int{{1, 6}}, 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.days, c.meetings)
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",
28+
c.expect, got, c.days, c.meetings)
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)