From 23960d1f9618f9fcd88a68f2f70b2c06c7780980 Mon Sep 17 00:00:00 2001 From: 0xff-dev Date: Mon, 24 Mar 2025 09:13:02 +0800 Subject: [PATCH] Add solution and test-cases for problem 3169 --- .../README.md | 43 +++++++++++++------ .../Solution.go | 22 +++++++++- .../Solution_test.go | 23 +++++----- 3 files changed, 63 insertions(+), 25 deletions(-) diff --git a/leetcode/3101-3200/3169.Count-Days-Without-Meetings/README.md b/leetcode/3101-3200/3169.Count-Days-Without-Meetings/README.md index 234b2b69d..bc2f05761 100755 --- a/leetcode/3101-3200/3169.Count-Days-Without-Meetings/README.md +++ b/leetcode/3101-3200/3169.Count-Days-Without-Meetings/README.md @@ -1,28 +1,47 @@ # [3169.Count Days Without Meetings][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 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). + +Return the count of days when the employee is available for work but no meetings are scheduled. + +**Note**: The meetings may overlap. **Example 1:** ``` -Input: a = "11", b = "1" -Output: "100" +Input: days = 10, meetings = [[5,7],[1,3],[9,10]] + +Output: 2 + +Explanation: + +There is no meeting scheduled on the 4th and 8th days. ``` -## 题意 -> ... +**Example 2:** + +``` +Input: days = 5, meetings = [[2,4],[1,3]] -## 题解 +Output: 1 + +Explanation: + +There is no meeting scheduled on the 5th day. +``` + +**Example 3:** -### 思路1 -> ... -Count Days Without Meetings -```go ``` +Input: days = 6, meetings = [[1,6]] +Output: 0 + +Explanation: + +Meetings are scheduled for all working days. +``` ## 结语 diff --git a/leetcode/3101-3200/3169.Count-Days-Without-Meetings/Solution.go b/leetcode/3101-3200/3169.Count-Days-Without-Meetings/Solution.go index d115ccf5e..7b4cf91b3 100644 --- a/leetcode/3101-3200/3169.Count-Days-Without-Meetings/Solution.go +++ b/leetcode/3101-3200/3169.Count-Days-Without-Meetings/Solution.go @@ -1,5 +1,23 @@ package Solution -func Solution(x bool) bool { - return x +import "sort" + +func Solution(days int, meetings [][]int) int { + ans := 0 + sort.Slice(meetings, func(i, j int) bool { + a, b := meetings[i], meetings[j] + if a[0] == b[0] { + return a[1] < b[1] + } + return a[0] < b[0] + }) + end := 0 + for i := 0; i < len(meetings); i++ { + if meetings[i][0] > end { + ans += meetings[i][0] - end - 1 + } + end = max(end, meetings[i][1]) + } + ans += days - end + return ans } diff --git a/leetcode/3101-3200/3169.Count-Days-Without-Meetings/Solution_test.go b/leetcode/3101-3200/3169.Count-Days-Without-Meetings/Solution_test.go index 14ff50eb4..ec80a03cf 100644 --- a/leetcode/3101-3200/3169.Count-Days-Without-Meetings/Solution_test.go +++ b/leetcode/3101-3200/3169.Count-Days-Without-Meetings/Solution_test.go @@ -9,31 +9,32 @@ import ( func TestSolution(t *testing.T) { // 测试用例 cases := []struct { - name string - inputs bool - expect bool + name string + days int + meetings [][]int + expect int }{ - {"TestCase", true, true}, - {"TestCase", true, true}, - {"TestCase", false, false}, + {"TestCase1", 10, [][]int{{5, 7}, {1, 3}, {9, 10}}, 2}, + {"TestCase2", 5, [][]int{{2, 4}, {1, 3}}, 1}, + {"TestCase3", 6, [][]int{{1, 6}}, 0}, } // 开始测试 for i, c := range cases { t.Run(c.name+" "+strconv.Itoa(i), func(t *testing.T) { - got := Solution(c.inputs) + got := Solution(c.days, c.meetings) 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", + c.expect, got, c.days, c.meetings) } }) } } -// 压力测试 +// 压力测试 func BenchmarkSolution(b *testing.B) { } -// 使用案列 +// 使用案列 func ExampleSolution() { }