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:
parent
50cfe9c234
commit
e810e992fe
1 changed files with 27 additions and 0 deletions
27
go/count-days-without-meetings.go
Normal file
27
go/count-days-without-meetings.go
Normal 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
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue