go: add «3169. Count Days Without Meetings»

URL:	https://leetcode.com/problems/count-days-without-meetings/
Signed-off-by: Matej Focko <me@mfocko.xyz>
This commit is contained in:
Matej Focko 2025-03-24 18:56:53 +01:00
parent 50cfe9c234
commit e810e992fe
Signed by: mfocko
SSH key fingerprint: SHA256:icm0fIOSJUpy5+1x23sfr+hLtF9UhY8VpMC7H4WFJP8

View file

@ -0,0 +1,27 @@
package main
import (
"cmp"
"slices"
)
func countDays(days int, meetings [][]int) int {
compareMeetings := func(a, b []int) int {
return cmp.Or(cmp.Compare(a[0], b[0]), cmp.Compare(a[1], b[1]))
}
slices.SortFunc(meetings, compareMeetings)
free, latestEnd := 0, 0
for _, meeting := range meetings {
start, end := meeting[0], meeting[1]
if start > latestEnd+1 {
free += start - latestEnd - 1
}
latestEnd = max(latestEnd, end)
}
free += days - latestEnd
return free
}