-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
90 lines (82 loc) · 2.04 KB
/
main.go
File metadata and controls
90 lines (82 loc) · 2.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
// Source: https://leetcode.com/problems/count-days-without-meetings
// Title: Count Days Without Meetings
// Difficulty: Medium
// Author: Mu Yang <http://muyang.pro>
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// 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: days = 10, meetings = [[5,7],[1,3],[9,10]]
// Output: 2
// Explanation:
// There is no meeting scheduled on the 4^th and 8^th days.
// ```
//
// **Example 2:**
//
// ```
// Input: days = 5, meetings = [[2,4],[1,3]]
// Output: 1
// Explanation:
// There is no meeting scheduled on the 5^th day.
// ```
//
// **Example 3:**
//
// ```
// Input: days = 6, meetings = [[1,6]]
// Output: 0
// Explanation:
// Meetings are scheduled for all working days.
// ```
//
// **Constraints:**
//
// - `1 <= days <= 10^9`
// - `1 <= meetings.length <= 10^5`
// - `meetings[i].length == 2`
// - `1 <= meetings[i][0] <= meetings[i][1] <= days`
//
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
package main
import (
"fmt"
"slices"
)
func countDays(days int, meetings [][]int) int {
slices.SortFunc(meetings, func(a, b []int) int {
if a[0] != b[0] {
return a[0] - b[0]
}
return a[1] - b[1]
})
count := 0
now := 0
for _, meeting := range meetings {
count += max(meeting[0]-now-1, 0)
now = max(now, meeting[1])
}
count += max(days-now, 0)
return count
}
func main() {
fmt.Println(countDays(
10,
[][]int{
{5, 7},
{1, 3},
{9, 10},
},
// 5,
// [][]int{
// {1, 3},
// {2, 4},
// },
))
}